tags:

views:

338

answers:

1

What is the Correct(TM) way to handle checkboxes with asp.net MVC and strongly typed views. I have a class 'Bar' that has a member 'foo'. 'foo' is type 'byte'.

Now what I would like do, is have something like this in controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Page( Bar bar ) 
{
   if (ModelState.IsValid)
   {    
      SaveChanges(bar);
 ...

And something like this in the view

<%=Html.CheckBox( "foo", Model.foo) %>

Of course the above code doesn't work, because DefaultModelBinder(?) doesn't know how to convert output from Checkbox (discussed here) to byte so ModelState.IsValid is false.

How to make it work? Custom Model Binders? How? Seems like a simple problem, but I've failed to find a comprehensive guide how to do this. (yes, I know I suck at googling :-)

+2  A: 

In your Model - convert the Byte to a Bool property. You can still store in the database as a byte. Just make the get/set use a bool and convert there.

GeekyMonkey