locked
Html.CheckBox BUG? RRS feed

  • Question

  • User-2014110218 posted

    This:

    1    <%=Html.CheckBox("paid", false) %>


    produces this:

     

    1    <input id="paid" name="paid" value="true" type="checkbox">
    2 <input name="paid" value="false" type="hidden">

    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:

    1    string paid = Request.Form["paid"];

     produces the output as:

    //paid = "true,false"
    Is this really wanted? Do I missunderstood something? 
      
    Wednesday, September 3, 2008 10:45 AM

Answers

  • User-159223474 posted

    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 Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 3, 2008 11:15 AM
  • User1220545395 posted

    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. ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 5, 2008 12:38 PM

All replies

  • User-159223474 posted

    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 Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 3, 2008 11:15 AM
  • User-2014110218 posted

    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() ?

    Thursday, September 4, 2008 2:22 AM
  • User-159223474 posted
    Yes, the UpdateModel method will work too. Check out this blog post by ScottGu on ModelBinders: http://weblogs.asp.net/scottgu/archive/2008/09/02/asp-net-mvc-preview-5-and-form-posting-scenarios.aspx.
    Thursday, September 4, 2008 3:19 AM
  • User874596136 posted

     You can use ModelBinders separately from UpdateModel(), if you need it.

    For example:

    object o = ModelBinders.GetBinder(typeof (bool)).GetValue(this.ControllerContext, "YourCheckboxName", typeof (bool), this.ViewData.ModelState);
    bool isChecked = o != null ? (bool)o : false;
      
    Friday, September 5, 2008 4:55 AM
  • User1220545395 posted

    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. ;)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 5, 2008 12:38 PM
  • User-839343 posted

     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.

     

    Thursday, September 18, 2008 6:01 PM
  • User-1660457439 posted

    Check Request.Form.GetValues("Test")[0].  This will produce just "true" or just "false" if you're using our <%= Html.CheckBox() %> helper.

    Thursday, September 18, 2008 8:27 PM