asp.net-mvccheckboxhtml-helpermvc-editor-templates

Disabling checkbox in mvc does not submit values to database


Is it that if a checkbox is disabled, the value does not get submitted to the database? I have disabled a checkbox so that even if the user does not enter any values in the rest of the form, atleast one value will be submitted. But when I click on the submit button there is no entry in the database. Please Help

<table class="table-bordered">
<tr>
    <td colspan="2">
        @Html.CheckBoxFor(m => m.isUsed, new { @disabled="disabled"})
    </td>
</tr>
<tr>
    <td>
         @Html.TextBoxFor(m => m.FirstName, @Model.isUsed ? (object)new { @class = "form-control" } : new { @class = "form-control", @disabled = "disabled" })
        @Html.ValidationMessageFor(m => m.FirstName)
    </td>
</tr>

Solution

  • It will not submit/pick the value from a disabled element. What you can do here is add a strongly type hidden field which will hold the value of the property. The model binder will do the rest i.e.

    @Html.HiddenFor(m => m.isUsed)
    // instead of..
    @Html.CheckBoxFor(m => m.isUsed, new { @disabled="disabled"})
    

    This way the Model.isUsed will have its value while submitting the form.