title: ASP.NET MVC - Using Check Boxes to Update Many to Many Relationships
body: <p>I am in a MVC project and am currently stuck with many-to-many mapping.</p>
  
  <p>I have a many to many relationship. It models a student course selection in a university. A student has subjects and for the given semester there is a complete list of subjects.</p>
  
  <p>Need: make a form that a student could visit the page and through the use of check boxes to select some subjects.</p>
  
  <p>I created a View Modle (at least that is what I call it). It is the model that will be used in the View 'Edit'.</p>
  
  <p>Edit.cshtml</p>
  
  <pre><code>//in the Edit View
  @model StudentViewModel
  
  //Some more code
  @Html.EditorFor(model =&gt; model.Std.Name)
  //etc..
  </code></pre>
  
  <p>The student view model</p>
  
  <pre><code>class StudentViewModel
  {
  public Student Std { get; set; }
  public List&lt;SubjectsSelection&gt; Subs{ get; set; }
  }
  
  class SubjectSelection
  {
  public string Name { get; set; }
  public int ID { get; set; }
  public bool Selected { get; set; }
  }
  </code></pre>
  
  <p>What I want is to add a set of check-boxes for each and every subject such that <code>Selected = true</code> will be checked.</p>
  
  <p>I was planning to create a list of check-boxes through a <code>for</code> loop manually. </p>
  
  <p>Can I do this with some thing similar for <code>@Html.EditorFor(model =&gt; model.Subs)</code> such that when the form is submitted I can use a controller like this</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(Machine machine, SubjectSelection[] subs)
  {
  //Some code here
  }
  </code></pre>
  
  <p>So that there after I will be able to make the necessary connections (at least hopefully) and save to the database.</p>