Skip to content

Model Binding Checkboxes to a List

Model binding things other than textboxes could be tricky in ASP.NET MVC. To correctly model bind, one has to follow a specific naming/value conventions in the HTML.

Let’s say we have a model object called People, and we have a List of states people have lived in.

  public class People
    public List StatesLiveIn { get; set; }

And in our View we have as follows:

@model TestBinder.Models.People

foreach (string state in new List() { "NY","NJ","FL","IL","TX" })
  @state <br />      

Out controller simple looks like this:

    public ActionResult Index()
      return View();

    public ActionResult Index(People p)
      return View(p);

If we put a trace point at the return line, and inspect after the user has submitted only NY and TX…

…we’ll see that it was correctly bound…

Dan View All

Blog owner.

%d bloggers like this: