Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.CheckBox returns false if disabled, even if seleced

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?

like image 734
elwyn Avatar asked Jan 17 '11 21:01

elwyn


People also ask

How do I make a checkbox checked and disabled in HTML?

PS...if you want the checkbox to be in the checked state you need to add checked="checked" , not mess with the javascript. The javascript is just there to force mouse clicks to be ignored on the input object, not to set state of the checkbox.

Does checkbox return true or false?

The Input Checkbox defaultChecked property in HTML is used to return the default value of checked attribute. It has a boolean value which returns true if the checkbox is checked by default, otherwise returns false.

How do I GREY out a checkbox in HTML?

You can style checkbox label and readonly inputs with CSS, e.g.: input [readonly="readonly"] {} but the browser should make the checkbox should appear greyed out when set to readonly. "checkbox itself should appear greyed out just by setting the readonly attribute" - Tried in IE8, FF12.


3 Answers

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.

like image 121
Darin Dimitrov Avatar answered Oct 06 '22 02:10

Darin Dimitrov


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>
    }
like image 26
LeoFraietta Avatar answered Oct 06 '22 02:10

LeoFraietta


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

like image 20
Ghini Antonio Avatar answered Oct 06 '22 04:10

Ghini Antonio