3

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>
6
  • The value should get stored in the DB. Please check what is happening in the server side Action. Does the model have the value. Commented Oct 1, 2015 at 5:26
  • Disabled form controls do not post a value. In your case a value of false will always be posted because of the associated hidden input generated by CheckBoxFor() Commented Oct 1, 2015 at 5:26
  • And your conditional statement in the TextBoxFor() method generates identical html attributes so unclear what that is for. Commented Oct 1, 2015 at 5:29
  • @StephenMuecke I just want to hide the checkbox or disable it so that the user cannot uncheck the checkbox. When the checkbox is unchecked the texbox is disabled and a blank value will be submitted for FirstName. Originally the TextBoxFor() method looks like this @Html.TextBoxFor(m => m.FirstName, @Model.isUsed ? (object)new { @class = "form-control" } : new { @class = "form-control", @disabled = "disabled" }) Commented Oct 1, 2015 at 5:46
  • 1
    You should edit you question to show the real code for the TextBoxFor() method. Just include @Html.HiddenFor(m => m.isUsed) so the value posts back and manually create a checkbox <input type="checkbox" disabled> if you really need to show a checkbox Commented Oct 1, 2015 at 5:54

3 Answers 3

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That will always post back false even if isUsed is true because the CheckBoxFor() method generates a hidden input with value="false"
Oh, surely it will. I thought OP has asked to always save the value in DB. My bad. I should have asked for further clarification.
@Rohit416 I removed the checkbox and instead kept only the HiddenFor() method. It works now as expected.
1

You have to remove 'disabled' property before submitting the form. Suppose your form id is 'TestForm' then,

$('#TestForm').submit(function() {
        $('#CheckBoxId').removeAttr('disabled');
    });

Comments

0

Another way to handle this without adding extra overhead to the front end page is to just use a default value of false/0 for the corresponding backend parameter. That way you don't care if the value was posted or not (depending on your app architecture of course).

i.e. If it's unchecked (and thus not posted) it ends up false at the backend. If it was checked and is posted, it overrides the false/0 value at the backend.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.