title: MVC 3 - Passing list of check boxes to and from a controller
body: <p>I have a list of information sources in a database that I need to pass to a view in MVC.  I need an end user to be able to tick the sources of information that apply to their course.</p>
  
  <p>I am able to successfully pass the view a list of information sources alongside check boxes using the following code.</p>
  
  <pre><code>public ViewResult CreateUpdateInfoSource(int ProgrammeId)
          {
              List&lt;ProgInfoSourceModel&gt; viewmodel = new List&lt;ProgInfoSourceModel&gt;();
              List&lt;ProgInfoSourceDTO&gt; myProgInfoDTOList = progInfoSourceService.AllInfoSources(); 
              if (myProgInfoDTOList.Count != 0)
              {
                  foreach (var x in myProgInfoDTOList)
                  {
                      ProgInfoSourceModel insert = new ProgInfoSourceModel();
                      insert.Selected = false;
                      insert.ProgrammeId = ProgrammeId;
                      insert.InfoSourceId = x.InfoSourceId;
                      insert.InfoSource = x.InfoSource;
                      insert.InfoReference = x.InfoReference;
                      insert.Rank = x.Rank;
  
                      viewmodel.Add(insert);
                  }
              }
  
              return View(viewmodel);
          }
  </code></pre>
  
  <p>I am able to unpack this in the view just fine, however I am having real difficulty passing a the list back to my controller.  I need to be able to loop through the list in my controller and see which ones do or don't apply so I can update the database.</p>
  
  <p>My model looks like this:</p>
  
  <pre><code>namespace ProgrammeSpec.MVC.Models
  {
      public class ProgInfoSourceModel
      {
          [DisplayName("Selected")]
          public bool Selected { get; set; }
          [DisplayName("Programme Id")]
          public int ProgrammeId { get; set; }
          [DisplayName("Info Source Id")]
          public int InfoSourceId { get; set; }
          [DisplayName("Info Source")]
          public String InfoSource { get; set; }
          [DisplayName("Reference")]
          public String InfoReference { get; set; }
          [DisplayName("Rank")]
          public int? Rank { get; set; }
      }
  }
  </code></pre>
  
  <p>My View looks like this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;CreateUpdateInfoSource&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  @using (Html.BeginForm())
  {
      &lt;table&gt;
          &lt;tr&gt;
              &lt;th&gt;
                  Selected
              &lt;/th&gt;
              &lt;th&gt;
                  ProgrammeId
              &lt;/th&gt;
              &lt;th&gt;
                  InfoSourceId
              &lt;/th&gt;
              &lt;th&gt;
                  InfoSource
              &lt;/th&gt;
              &lt;th&gt;
                  InfoReference
              &lt;/th&gt;
              &lt;th&gt;
                  Rank
              &lt;/th&gt;
              &lt;th&gt;&lt;/th&gt;
          &lt;/tr&gt;
  
      @foreach (var item in Model)
      {
          &lt;tr&gt;
              &lt;td&gt;
                  @Html.CheckBoxFor(modelItem =&gt; item.Selected)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.ProgrammeId)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.InfoSourceId)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.InfoSource)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.InfoReference)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.DisplayFor(modelItem =&gt; item.Rank)
              &lt;/td&gt;
              &lt;td&gt;
                  @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                  @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                  @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
              &lt;/td&gt;
          &lt;/tr&gt;
      }
  
  
      &lt;/table&gt;
      &lt;input type="submit" value="Update" /&gt;
  }
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and the controller that the view gets passed to looks like this: (snippet)</p>
  
  <pre><code>[HttpPost]
          public ActionResult CreateUpdateInfoSource(List&lt;ProgInfoSourceModel&gt; viewmodel)
          {
              if (ModelState.IsValid)
              {
                  try
                  {
  </code></pre>
  
  <p>The problem is the viewmodel is null.  I understand this is probably because I've unpacked the list in the view so it is no longer a list but how can I access the values of the check boxes then?</p>
  
  <p>The added complication is that the number of info sources will vary so I can't use a static form or list and give each one an Id... </p>
  
  <p>This must be a fairly common problem with a simple solution, but I'm an MVC novice and I don't know how to get round this.</p>