AJAY cHIGURUPATI AJAY cHIGURUPATI - 17 days ago 5
Javascript Question

How to post checkbox value when disabled

I tried to put readonly property for checkbox,but it is allowing user to check/uncheck the field.

Later I tried to put disable which is preventing user from check/uncheck.

But when I tried to submit form,the checkbox with disbaled value is not passing to MVC controller.

I tried to put hidden value,and tried to set value when changed but not able to pass the value to controller.

Here is my Code:

<div style="width:200px">@Html.CheckBoxFor(m => m.In, new { id = "cbIn", style = "vertical-align:middle;", data_bind = "checked:In,disable:useSettings" })</div>
@Html.HiddenFor(m=>m.In,new { id = "hiddenIn" })


JQuery Code:

$("#hiddenIn").val(data.In);

function SaveSettings() {
try {

var saveSettingsUrl = "/Settings/SaveSettings";
var serializedForm = $("input,select").serialize();

$.ajax({
type: 'POST',
url: saveSettingsUrl,
async: true,
data: serializedForm,
success: function (data, textStatus, request) {

data = null;

},
error: function (request, status, error) {
handleException(request.responseText);
}
});
}
catch (error) {
showExceptionWindow('Jquery Error:' + error);
}
}


How can I send the checkbox value,even when it is disabled in form post.

Answer

You've already the hidden input :

@Html.HiddenFor(m=>m.In,new { id = "hiddenIn" })

That should pass the value of disabled checkbox.


Solution without hidden input, you could remove the disabled attribute before getting the elements values from the form :

$("input:checkbox").prop('disabled', false);
//Then get the data
var serializedForm = $("input,select").serialize();

Hope this helps.

Comments