Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Please explain Html.CheckBoxFor() rendering

Reply
Thread Tools

Please explain Html.CheckBoxFor() rendering

 
 
J055
Guest
Posts: n/a
 
      05-24-2010
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


 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      05-25-2010
On May 24, 7:53*pm, "J055" <j...@newsgroup.nospam> 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
 
Reply With Quote
 
 
 
 
J055
Guest
Posts: n/a
 
      05-27-2010

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Please explain this "Why's" example please Kaye Ng Ruby 8 06-08-2010 09:13 AM
I can't explain why my scripts run different (even with differentstyle rendering) on two machines Gotch@ HTML 1 12-12-2008 01:56 PM
IE6 SP1 rendering vs IE6 SP2 rendering Peter Mount HTML 4 01-31-2006 08:01 AM
Someone please explain to me ... =?Utf-8?B?bXJzLmdyYW50?= Wireless Networking 6 11-25-2004 10:08 PM
please help... ...me learn C++ please please please :) KK C++ 2 10-14-2003 02:08 PM



Advertisments