CheckBoxFor(t => t.boolValue, new { disabled="disabled" }) method to render a checkbox, in disabled mode.
The method renders a hidden field as well.
My question is why does this hidden field has a false value for disabled check box? I believe the purpose of the hidden field is to have some extra behavior over the default check box behavior
Is there a way to override default MVC functionality so that the value of this hidden field is based on the state of the checkbox even in disabled mode?
The hidden field is used to bind the checkbox value to a boolean property. The thing is that if a checkbox is not checked, nothing is sent to the server, so ASP.NET MVC uses this hidden field to send false and bind to the corresponding boolean field. You cannot modify this behavior other than writing a custom helper.
This being said, instead of using
disabled="disabled"
usereadonly="readonly"
on the checkbox. This way you will keep the same desired behavior that the user cannot modify its value but in addition to that its value will be sent to the server when the form is submitted:UPDATE:
As pointed out in the comments section the
readonly
attribute doesn't work with Google Chrome. Another possibility is to use yet another hidden field and disable the checkbox:UPDATE 2:
Here's a full testcase with the additional hidden field.
Model:
Controller:
View:
When the form is submitted the value of the Foo property is
true
. I have tested with all major browsers (Chrome, FF, IE).Here is a quick tip I discovered : If you set a default value for the boolean value on the object. It's possible to write out the tag with the value as true/false (this value should be the opposite of the default boolean value on the object)
The value is only sent if the checkbox is checked, so it works.
I find the added parameter in the query string looks a mess and makes people think that we as developers have done something wrong. So I've resorted to the extreme method of creating my own InputExtensions class which allows me to decide on whether I want the hidden input to be rendered or not.
The following is the InputExtensions class I've created (based on the existing MVC code to maintain full compatibility):
Then you can call the method like so:
Although Darin's answer is perfectly fine, there is another way of making sure that disabled checkbox post backs as expected.
This solution will also work in the scenarios where checkbox is conditionally disabled based on a property in a model, or enabled/disabled client-side based on user selection or client-side retrieved data.
You can keep your view simple: (setting disabled here is optional)
This will, as Darin mentioned generate an extra hidden input field set to false, so that the checkbox value will post-back also when checkbox is not checked. Therefore we know that
Something
field will always post-back regardless if checkbox is checked or not, disabled or not. The only problem that if checkbox is disabled it will always post back asFalse
.We can fix that with some javaScript post processing before submitting. In my case the code looks something along those lines:
You may need to modify it for your scenario, but you should get the idea.