Mvc 4 Editorfor Bool Checkbox Always Posting True
I have a bool property in my model. Its value is false when rendering the view. But when I submit the form, the value of TRUE is being passed to the controller. I am using a custom
Solution 1:
try
functionGetFilterCriteria() {
varCriteria = {};
$('#frmCriteria input').each(function () {
Criteria[this.name] = false;
if ($("#" + this.name).is(':checked'))
Criteria[this.name] = true;
});
returnCriteria;
};
Solution 2:
Thanks all for the hints...in the end it had to be a bit more complex due to the way the razor creates 2 input controls for the checkbox. So here is what I had to do:
functionGetFilterCriteria() {
varCriteria = {};
$('#frmCriteria input').each(function () {
if ($(this).attr('type') != 'hidden') {
if ($(this).attr('type') == 'checkbox') {
Criteria[this.name] = this.checked;
} else {
Criteria[this.name] = $("#" + this.name).val();
}
}
});
returnCriteria;
};
I don't like it because if I ever want a hidden input, this will skip that. But for now it works. I find it hard to believe that it has to be this difficult :(
Solution 3:
you can try using .CheckBoxFor()
you will get an input element only.
for the checked property you can use:
$('#CurrentMailTemplateEnabled').get()[0].checked
to get the value by getting the DOM element first. At least that worked for me.
Post a Comment for "Mvc 4 Editorfor Bool Checkbox Always Posting True"