Html.CheckBox returns false if disabled, even if seleced
Asked Answered
S

7

34

Hopefully an easy question for you asp.net mvc gurus:

I have a checkbox, created like this:

<%=Html.CheckBox("MyCheckBox", true, new { disabled = "disabled"})%>

In my action I am checking the value like so:

bool isChecked = form["MyCheckBox"].Contains("true");

I expect this to return true, as it is checked. However the hidden element that gets created has a false value:

<input checked="checked" disabled="disabled" id="MyCheckBox" name="MyCheckBox" type="checkbox" value="true" />
<input name="MyCheckBox" type="hidden" value="false" />

First, is there a way to make the HtmlHelper behave as I expect it should? Or is manually constructing the input/creating my own helper method the only way? (not that this is a big deal...)

Second, can anyone shed some light on why the checkboxes behave this way? Am I incorrect in assuming a disabled checkbox that is ticked should == true? Does a disabled state semantically mean false?

Sepalous answered 17/1, 2011 at 21:58 Comment(1)
Found solution here: how to POST/Submit an Input Checkbox that is disabled?Documentary
B
44

An input field marked with the disabled="disabled" attribute NEVER sends its value to the server. If you want to achieve this you could use the readonly="readonly" attribute which still prevents the user from modifying the value but sends the existing value when the form is submitted.

Buddhology answered 17/1, 2011 at 22:0 Comment(9)
+1: I found this out the hard way not that long ago as I am also a beginner in MVC. However, do you have a good suggestion if it is an input field and I want to make it disabled/readonly? Currently setting "readonly" does NOT grey the input field like it does for the "disabled".Pearlstein
@VoodooChild, yeah that happens with FireFox and checkboxes. Common workarounds involve disabled with a backing hidden field.Buddhology
Thanks, I did not know that. I went with a custom HtmlHelper that uses the 'disabled' attribute and renders a hidden field.Sepalous
@elwyn: could you please post sample of your work around when you get a chance? I would like to keep it for a reference on this question. Thanks!Pearlstein
Sorry the extension method I made is written for a very specific situation (it isn't just a CheckBox helper), the problem I gave in the OP is a simplified case. What I wrote wouldn't be much use to anyone else...Sepalous
I've tried 'readonly' with checbox. There is no effect if I make it read only.Mickimickie
@gmailuser, then probably you have some other problem.Buddhology
@DarinDimitrov readonly attribute does not work on checkboxes in general.Essayistic
@gmailuser I ended up getting around that by using IValidatable to perform validation if the user didn't select the options I wanted them toSold
S
6

I don't know if it solves your problem but this was the solution for me:

My model has an UserMigrated boolean property, once the user has setted to migrated, he can't go back, so the view becomes:

@if (Model.UserMigrated)
    {
    <td>@Html.CheckBox("chkBoxReadOnly", true, new {disabled = "disabled"})</td>
        @Html.HiddenFor(m => m.UserMigrated)
    }
else
    {
    <td>@Html.CheckBoxFor(m => m.UserMigrated)</td>
    }
Southard answered 13/2, 2019 at 18:2 Comment(0)
G
2

1) From my point of view there is no way to change the behavior of the Html.CheckBox method. I had to face the problem time ago and i ended up with manually constructing the input checkbox as you suggest. To construct the input checkbox is not always the hell as using the helper methods is not always the eaven. Of course biulding your own helper method can be the right choise.

2) Disabled input field is not posted when form is submitted; i would expect the same, submiting disabled input checkbox fileds, whether are checked or not.

We can argue about the choice adoped to relay on input hidden field for pushing into the request the checkbox, when the checkbox is not selected. The side effect of this choice is that we will find the checkbox name among the the posted data even if the checkbox is disable in disagreement with what stated above that is: disabled input field is not posted when form is submitted

Griffen answered 6/11, 2013 at 8:45 Comment(0)
B
2

Adding the Hidden field before check box worked for me. Note that i am adding the hidden field only if the field is disabled.

Credit goes to this post

  @{
     if (Model.MyFieldDisabled)
      {
         @Html.HiddenFor(x => x.MyField)
      }
   }    

    @Html.CheckBoxFor(x => x.MyField,  new {disabled = "disabled"})
Breaker answered 24/4, 2020 at 9:59 Comment(1)
LOL, this is the best solution.Crafty
A
0

I faced a similar issue when trying to use disabled for a checkbox. My solution was just to not display the checkbox since the user did not have the access to modify the field.

I also made sure the Html.HiddenFor was there so the value was retained.

Abigailabigale answered 18/9, 2018 at 22:31 Comment(1)
Yes, not sure what is a point of disabled controlled, it should have been label, or just not be shown. there is not point in displaying a control where it is not being used, or can not be used for anything except to display information.Stylobate
E
0

In my case below was the scenario, it was to do with asp-tag helpers. So to send the value, I have used input hidden fields and it worked.

<input type="checkbox" asp-for="@Model.SomeList[i].Selected" disabled />
<input type="hidden" asp-for="@Model.SomeList[i].Selected" />

HTH to others :)

Effectuate answered 30/12, 2020 at 18:49 Comment(0)
C
0
@(Model.IsClosed ? "Yes" : "No")
@Html.HiddenFor(m => m.IsClosed)

This worked for me in all cases. No need of disable and readonly

Cadmus answered 19/7, 2021 at 14:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.