This isn't a bug, and is in fact the same approach that both Ruby on Rails and MonoRail use.
When you submit a form with a checkbox, the value is only posted if the checkbox is checked. So, if you leave the checkbox unchecked then
nothing will be sent to the server when in many situations you would want
false to be sent instead. As the hidden input has the same name as the checkbox, then if the checkbox is unchecked you'll still get a 'false' sent to the server.
When the checkbox is checked, the ModelBinder will automatically take care of extracting the 'true' from the 'true,false'
Marked as answer by dimi3 on Sep 04, 2008 06:18 AM
That also allows a checkbox to correspond to a bool parameter of an action method. For exmaple, if you have <%= Html.CheckBox("discontinued", ...) %> and post it to an action method
public ActionResult DoSomething(bool discontinued) {...}
It will work. Without that hidden input, if you unchecked the box and posted it, it would fail.In my opinion, this is the result of a bug, but the bug is in how the HTML checkbox itself works. I wish it would post an empty value in the form if it's unchecked,
but this is the world we live in and we have to make do. ;)
Phil Haack (http://haacked.com/)
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
Marked as answer by tgmdbm on Sep 12, 2008 09:40 PM
dimi3
Member
318 Points
166 Posts
Html.CheckBox BUG?
Sep 03, 2008 02:45 PM|LINK
This:
<%=Html.CheckBox("paid", false) %>produces this:
What's the purpose of the hidden input?
Anyway, when the form is posted and the checkbox is checked, retrieving Request.Form["paid"] in the Controller as this:
produces the output as:
//paid = "true,false"JeremyS
Member
506 Points
99 Posts
Re: Html.CheckBox BUG?
Sep 03, 2008 03:15 PM|LINK
This isn't a bug, and is in fact the same approach that both Ruby on Rails and MonoRail use.
When you submit a form with a checkbox, the value is only posted if the checkbox is checked. So, if you leave the checkbox unchecked then nothing will be sent to the server when in many situations you would want false to be sent instead. As the hidden input has the same name as the checkbox, then if the checkbox is unchecked you'll still get a 'false' sent to the server.
When the checkbox is checked, the ModelBinder will automatically take care of extracting the 'true' from the 'true,false'
dimi3
Member
318 Points
166 Posts
Re: Html.CheckBox BUG?
Sep 04, 2008 06:22 AM|LINK
Thank you for the answer. Until now, I used Substring(0,4) to make the comparison.
Can you provide an example with "ModelBinder"? Did you mean the method UpdateModel() ?
JeremyS
Member
506 Points
99 Posts
Re: Html.CheckBox BUG?
Sep 04, 2008 07:19 AM|LINK
dinoboy
Member
542 Points
105 Posts
Re: Html.CheckBox BUG?
Sep 05, 2008 08:55 AM|LINK
You can use ModelBinders separately from UpdateModel(), if you need it.
For example:
Haacked
Contributor
6901 Points
412 Posts
Re: Html.CheckBox BUG?
Sep 05, 2008 04:38 PM|LINK
That also allows a checkbox to correspond to a bool parameter of an action method. For exmaple, if you have <%= Html.CheckBox("discontinued", ...) %> and post it to an action method
public ActionResult DoSomething(bool discontinued) {...}
It will work. Without that hidden input, if you unchecked the box and posted it, it would fail.In my opinion, this is the result of a bug, but the bug is in how the HTML checkbox itself works. I wish it would post an empty value in the form if it's unchecked, but this is the world we live in and we have to make do. ;)
Senior Program Manager, Microsoft
What wouldn’t you do for a Klondike bar?
jyoung1024
Member
14 Points
37 Posts
Re: Html.CheckBox BUG?
Sep 18, 2008 10:01 PM|LINK
My co-worker just ran across this and showed it to me. I thought it was a bug as well, and tried it and got the same results.
Request.Form["Test"] = "true,false"
So what if you are not using ModelBinders and just want / need to use Request.Form, what is the best way to get the value?
Using a substring or contains "true" seems like a bad hack.
levib
Star
7702 Points
1099 Posts
Microsoft
Re: Html.CheckBox BUG?
Sep 19, 2008 12:27 AM|LINK
Check Request.Form.GetValues("Test")[0]. This will produce just "true" or just "false" if you're using our <%= Html.CheckBox() %> helper.