title: MVC Model with a list of objects as property
body: <p>I am working on an MVC application where the Model class <code>Item</code> has a <code>List&lt;Colour&gt;</code> named <code>AvailableColours</code> as a property.</p>
  
  <p><code>AvailableColours</code> is a user defined subset of <code>Colour</code> classes. I would like to display all <code>Colour</code> instances in a check box list, and when submitted, <code>AvailableColours</code> is a <code>List&lt;Colour&gt;</code> containing the checked <code>Colour</code> classes.</p>
  
  <p>What is the best way to do this in MVC?</p>
  
  <p>Edit: My code so far, although I feel this is not the most MVC-ish way to do it!</p>
  
  <p><strong>Model</strong></p>
  
  <pre><code>public class Item
  {
      public int ID { get; set; }
      public List&lt;Colour&gt; AvailableColours { get; set; }
  }
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>@model MyNamespace.Models.Item
  @using MyNamespace.Models;
  @{
      ViewBag.Title = "Create";
  
      var allColours = new List&lt;Colour&gt;(); //retrieved from database, but omitted for simplicity
  }
  
  &lt;h2&gt;Create New Item&lt;/h2&gt;
  
  @using (Html.BeginForm("Create", "Item", FormMethod.Post)) 
  {
      &lt;div&gt;
          @Html.LabelFor(model =&gt; model.AvailableColours)
  
          @foreach (var colour in allColours)
          {
  
             &lt;input type="checkbox" name="colours" value="@colour.Description" /&gt;
          }
      &lt;/div&gt;
  
      &lt;input type="submit" value="Submit" /&gt;
  }
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(Item item, string[] colours)
  {
      try
      {
          foreach (var colour in colours)
          {
              item.AvailableColours.Add(GetColour(colour));//retrieves from database
  
              return RedirectToAction("Index");
          }
      }
      catch
      {
         return View();
      }
  }
  </code></pre>