How to pass value of retain stuff to submit function ?

Ahmed Salah Abed Elaziz 390 Reputation points
2024-01-21T13:58:24.5933333+00:00

I work on asp.net MVC . I can't pass value of retain stuff checkboxes to submit function Yes have id RetainStuffTrue and it is value True No have id RetainStuffFalse and it is value False if session role code not equal LM then the checkboxes retain stuff will not display so retain stuff display based on session is LM or not submit function will execute when click approve button so any one can help me how to pass value to retain stuff to submit if yes then true if No then false

 @if (Session[BLL.Common.SessionKeys.RoleCode].ToString() == "LM" )
 {
 <table style="border: 1px solid black;width:100%;margin-bottom:5px;">

     <tr>
         <td style="width: 50%; font-weight: bold; padding-top: 10px;">
           
         </td>



         <td style="font-weight: bold; padding-top: 10px;">
            
     </tr>
     <tr>
         <td style="width: 50%; font-weight: bold; padding-top: 10px;">
             @Html.Label("Did you try to retain the staff?", htmlAttributes: new { @class = "control-label col-md-5" })
             <div class="col-md-7">
                 <input type="checkbox" id="RetainStuffTrue" name="RetainStuff" value="true" class="retaindtuff-checkbox" @(Model.RetainStuff == true ? "checked" : "") />
                 Yes
                 &nbsp;&nbsp;
                 <input type="checkbox" id="RetainStuffFalse" name="RetainStuff" value="false" class="retaindtuff-checkbox" @(Model.RetainStuff == false ? "checked" : "") />
                 No
             </div>
         </td>
         <td style="font-weight: bold; padding-top: 10px;">
            
         </td>
     </tr>
     <tr style="margin-bottom:5px;padding-bottom:5px;">
         

     </tr>


 </table>
 }

 <a id="approveControlsId" onclick="submit();" class="btn btn-primary" style="min-width: 100px;margin-top:5px;"><i class="glyphicon glyphicon-ok"></i> Approve </a>

<script type="text/javascript">
  function submit() {
      }
</script>

on jQuery i handle event change checkbox by jquery below

$('.retaindtuff-checkbox').on('change', function () {
    $('.retaindtuff-checkbox').not(this).prop('checked', false);
    $(this).val(this.checked);
});
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,590 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,277 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,551 Reputation points
    2024-01-21T15:40:06.2+00:00

    I would use radio buttons.

    A1

    @page
    @model IndexModel
    @{
        ViewData["Title"] = "Home page";
    }
    <style>
        legend {
            border: solid 1px black;
            -webkit-border-radius: 8px;
            -moz-border-radius: 8px;
            border-radius: 8px;
            padding: 6px;
        }
    
        .ledgend-special {
            width: 12em;
        }
    
        .ledgend-span {
            padding-left: .5em;
            padding-right: .5em;
            font-size: 1.5em;
        }
    </style>
    <div class="container mt-4">
        <form method="post">
            
            <fieldset class="border border-black p-3 bg-light" style="width: 20em;">
                <legend class="float-none w-auto fs-6">Did you try to retain the staff?</legend>
                @{
                    <div class="form-check">
    
                        <input type="radio"
                               class="form-check-input"
                               asp-for="UserResponse"
                               value="Yes"
                               id="Yes" />
    
                        <label class="form-check-label" for="Yes">Yes</label>
                    </div>
                    <div class="form-check">
    
                        <input type="radio"
                               class="form-check-input"
                               asp-for="UserResponse"
                               value="No"
                               id="No" />
    
                        <label class="form-check-label" for="No">No</label>
                    </div>
                }
            </fieldset>
    
            <div>
                <input type="submit" value="Submit" class="btn btn-primary mt-2"/>
            </div>
    
        </form>
    </div>
    

    Code behind (logging is done using SeriLog).

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using Serilog;
    
    namespace RadioButtonsApp.Pages;
    public class IndexModel : PageModel
    {
        public void OnGet()
        {
    
        }
    
    
        [BindProperty]
        public string? UserResponse { get; set; }
    
        public string[] Answers = { "Yes", "No"};
        public async Task<IActionResult> OnPost()
        {
            await Task.Delay(0);
            var selection = UserResponse;
            if (string.IsNullOrEmpty(selection))
            {
                Log.Information("No selection");
            }
            else
            {
    
                Log.Information("Choice is {C1}", selection.ToLower() == "yes");
            }
    
            return Page();
        }
    }
    
    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 70,776 Reputation points
    2024-01-21T22:50:54.4766667+00:00

    to make checkbox act like radio buttons:

    $('[name=RetainStuff]').on('click', function () {
        this.checked = true;
        $('[name=RetainStuff]').not(this).prop('checked', false);
    });
    

    to get the submit value a true/false for json its just:

    const submitValue = $('[name="RetainStuff"]:checked').val() == "true";
    
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.