Please explain Html.CheckBoxFor() rendering

Discussion in 'ASP .Net' started by J055, May 24, 2010.

  1. J055

    J055 Guest

    Hi

    When I use this:

    <%= Html.CheckBoxFor(m => m.CopyAddress)%>

    it produces html like:

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

    or

    <input id="CopyAddress" name="CopyAddress"
    type="checkbox" value="true" /><input name="CopyAddress" type="hidden"
    value="false" />

    which means the System.Web.Mvc.FormCollection for value CopyAddress are
    either "false" or "true,false".

    Can someone explain the logic and provide an example of how to use it to get
    either a true or false value?

    Thanks
    Andrew
    J055, May 24, 2010
    #1
    1. Advertising

  2. On May 24, 7:53 pm, "J055" <> wrote:
    > Hi
    >
    > When I use this:
    >
    > <%= Html.CheckBoxFor(m => m.CopyAddress)%>
    >
    > it produces html like:
    >
    >                     <input checked="checked" id="CopyAddress"
    > name="CopyAddress" type="checkbox" value="true" /><input name="CopyAddress"
    > type="hidden" value="false" />
    >
    > or
    >
    >                     <input id="CopyAddress" name="CopyAddress"
    > type="checkbox" value="true" /><input name="CopyAddress" type="hidden"
    > value="false" />
    >
    > which means the System.Web.Mvc.FormCollection for value  CopyAddress are
    > either "false" or "true,false".
    >
    > Can someone explain the logic and provide an example of how to use it to get
    > either a true or false value?
    >
    > Thanks
    > Andrew


    Please check this thread
    http://forums.asp.net/t/1314753.aspx
    Alexey Smirnov, May 25, 2010
    #2
    1. Advertising

  3. J055

    J055 Guest

    Thanks

    Rendering a dumb hidden field for every checkbox is a messy business. I
    guess if model binding requires it then so be it. I'm not using model
    binding at the moment so I created this function to handle bool values in
    the FormCollection when using Html.CheckBoxFor.

    private static bool CoerceToBool(string s)
    {
    if (string.IsNullOrEmpty(s)) return false;

    var a = s.Split(',');
    foreach (string t in a)
    {
    bool value;
    if (bool.TryParse(t, out value) && value) return true;
    }
    return false;
    }

    Cheers
    Andrew
    J055, May 27, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter Mount
    Replies:
    4
    Views:
    928
    Peter Mount
    Jan 31, 2006
  2. Sandros
    Replies:
    3
    Views:
    371
    GIMME
    Nov 5, 2004
  3. KK
    Replies:
    2
    Views:
    515
    Big Brian
    Oct 14, 2003
  4. Gotch@
    Replies:
    1
    Views:
    382
    Adrienne Boswell
    Dec 12, 2008
  5. Kaye Ng
    Replies:
    8
    Views:
    163
    Josh Cheek
    Jun 8, 2010
Loading...

Share This Page