AMDI
AMDI

Reputation: 973

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.

Upvotes: 2

Views: 4308

Answers (2)

Bharat
Bharat

Reputation: 2464

You can use like this, so use can not change state of checkbox and you can get value from check box as well by just adding onclick="return false;"

<input type="checkbox" checked onclick="return false;" onkeydown="return false;"/>

And in your razor syntax would be

@Html.CheckBoxFor(m => m.In, new { id = "cbIn", style = "vertical-align:middle;", data_bind = "checked:In,disable:useSettings", onclick="return false;"})

Upvotes: 1

Zakaria Acharki
Zakaria Acharki

Reputation: 67505

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.

Upvotes: 2

Related Questions