views:

110

answers:

3

I am doing:

convert.toboolean(request.form["mycheckbox"]

but since the value is 'false', it doesn't seem to cast to boolean.

What should I do?

I don't like checking for == "false" and then converting to boolean.

is there a cleaner way?

Update

All I am doing is:

if (Convert.ToBoolean(request.Form["somecheckbox"]))
{


}
A: 

bool b = request.form["mycheckbox"] != null && request.form["mycheckbox"] == "true";

chilltemp
A: 

Convert.ToBoolean is to be used for "0" and "1"

You can also try Boolean.Parse for "true" and "false"

rockinthesixstring
That's what I was thinking too, but then looked it up (http://msdn.microsoft.com/en-us/library/86hw82a3.aspx) there is a signature for ToBoolean(string).
jfrobishow
+1  A: 

Looks like the Html.Checkbox helper creates a checkbox and a hidden field, both with the name that you provide ("mycheckbox"). The hidden field appears to have the original value of the checkbox (though I could be off on what, exactly, its purpose is).

I would say that if you're getting values manually out of the Request collection, you should be creating your controls manually, too, instead of using Html.Checkbox and similar helpers, which may add other stuff that the framework knows about but you don't.

The other alternative would be to let the framework bind that value, rather than getting it manually.

bdukes