I work on asp.net MVC app . I face issue checkbox value not detected
changing value from true to false or reverse on model property speakstuff
.
so I need if yes checked then true if no checked then false then affect this value on model property speakstuff
.
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>
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>
jQuery code for single check
$(document).ready(function () {
$('.speak-stuff-checkbox').on('change', function () {
$('.speak-stuff-checkbox').not(this).prop('checked', false);
});
});
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>
}
[HttpPost]
public async Task<ActionResult> ApprovalIndex(ResignationRequester REQ)
{
}
So How to solve this issue ?
updated post
model that represent property 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; }
}
create a hidden field for SpeakStuff
@Html.HiddenFor(x => Model.SpeakStuff)
Remove The SpeakSuff property in the checkbox
<div class="col-md-7">
<input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="true" class="speak-stuff-checkbox") />
Yes
<input type="checkbox" id="SpeakStuff" name="SpeakStuff" value="false" class="speak-stuff-checkbox") />
No
</div>
Amend your javascript to update the value to the hidden field upon check selection
$(document).ready(function () {
$('.speak-stuff-checkbox').on('change', function () {
$('.speak-stuff-checkbox').not(this).prop('checked', false);
var res = $(this).val();
$('#Model_SpeakStuff').val($(this).val());
//alert($('#test').val());
});
});
The speakstuff value is derived from the hidden field.