My checkboxes are updating the db but on refresh the “checked” value is not there.
<input type="checkbox" id="chkPriorityOnly" @if(Model.PriorityOnly){<text> checked="checked" </text>}/> Priority Only <br />
Then as suggested, I used:
@Html.CheckBoxFor(model => Model.PriorityOnly, new { id = "chkPriorityOnly" }) Priority Only<br />
And this is my html upon saving the selections:
<input id="chkPriorityOnly" name="PriorityOnly" type="checkbox" value="true" /><input name="PriorityOnly" type="hidden" value="false" /> Priority Only<br />
And here is my save:
function OnRestrictionsSaveClick() {
$.ajax({
type: 'POST',
url: '@Url.Content("~/Audience/UpdateRestrictions?audienceID=" + Model.AudienceID)'
+ '&priorityOnly=' + $('#chkPriorityOnly:checked').val()
+ '&home=' + $('#chkHome:checked').val()
+ '&work=' + $('#chkWork:checked').val()
+ '&cell=' + $('#chkCell:checked').val()
+ '&text=' + $('#chkText:checked').val()
+ '&other=' + $('#chkOther:checked').val() ,
success: function (data) {SaveRestrictionsResponse(data); }
});
}
I don’t know if this will answer your question, but what you are doing in javascript isn’t correct.
The $(“*:checked”) selector will look for the check boxes that are checked.
Meaning $(“#chkHome:checked”) will return null if the checkbox isn’t checked.
Uing .val() will retrieve the value attribute of an input. Calling $(“#chkHome:checked”).val() will return “true” (the value attribute of the input) if it’s checked, otherwise null.
You should use $(“#chkHome”).is(“:checked”) (returns true if checked, otherwise false) instead.
Don’t have the hidden input, rather use:
It’s also probably better if the name attribute matches the property name on the model (ex. PriorityOnly vs chkPriorityOnly) for databinding purposes.