I work on asp.net MVC app . I face issue checkbox value not detected changing value from true to false or reverse .
meaning if I change checkbox Value from Yes to No OR No to Yes nothing detected according to speakstuff property model .
speakstuff
Exactly I need when checkbox check for yes then true and if check box for No will be false .
html script represent yes or No check box
<table style="border: 1px solid black;width:100%;margin-bottom:5px;"> <tr> <td style="width: 50%; font-weight: bold; padding-top: 10px;"> @Html.Label("Did You Meet/Speak to Staff", htmlAttributes: new { @class = "control-label col-md-5" }) <div class="col-md-7"> <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox" @(Model.SpeakStuff ? "checked" : "") /> Yes <input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox" @(!Model.SpeakStuff ? "checked" : "") /> No </div> </td> </table>
And below is jQuery to allow only selection with yes or no
<script> $(document).ready(function () { $('.speak-stuff-checkbox').on('change', function () { $('.speak-stuff-checkbox').not(this).prop('checked', false); }); });
I access value from form by java script
<script type="text/javascript"> function submit() { var ResignationRequester = new Object(); ResignationRequester.SpeakStuff = document.getElementById("SpeakStuff").value; </script>
model that represent property SpeakStuff
SpeakStuff
public class ResignationRequester { [Display(Name = "Employee No : ")] [Required] public int EmpID { get; set; } [Display(Name = "Request No")] public int RequestNo { get; set; } public bool SpeakStuff { get; set; } }
and When click approve button and check speakstuff property it still not changed
using (Html.BeginForm("ApprovalIndex", "Resignation", new { id = Model.RequestNo }, FormMethod.Post, htmlAttributes: new { @style = "display:inline;" })) { <a onclick="submit();" class="btn btn-primary" style="min-width: 100px;margin-top:5px; margin-left: 1300px;"><i class="glyphicon glyphicon-ok"></i> Approve </a> }
when debug model ResignationRequester speakstuff property it still true on all cases and if I change check box selection it still without change .
ResignationRequester speakstuff
//post action method type public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ) { }
So How to solve this issue ?