title: Contoso mvc3 system, many to many relationship
body: <p>I have been following the Microsoft Contoso MVC3 University Tutorial located <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application" rel="nofollow noreferrer">here</a> </p>
  
  <p>I have a question regarding one area, In part 6; the tutorial explains how to create a table of courses an instructor can be assigned to using checkboxes. Specifically from the heading “adding Course Assignments to the Instructor Edit Page” onward</p>
  
  <p>It seems overly complex, is there a more efficient way of doing things? Plug-in/built-in system etc.
  What if you wanted to expand the system so an instructor had more than just Courses assigned to him? The duplication of code would be huge.</p>
  
  <p>InstructorController</p>
  
  <pre><code>    // GET: /Instructor/Edit/5
      public ActionResult Edit(int id)
      {
          Instructor instructor = db.Instructors
              .Include(i =&gt; i.OfficeAssignment)
              .Include(i =&gt; i.Courses)
              .Where(i =&gt; i.InstructorID == id)
              .Single();
          PopulateAssignedCourseData(instructor);
          return View(instructor);
  
      }
  
      private void UpdateInstructorCourses(string[] selectedCourses, Instructor instructorToUpdate)
      {
          if (selectedCourses == null)
          {
              instructorToUpdate.Courses = new List&lt;Course&gt;();
              return;
          }
  
          var selectedCoursesHS = new HashSet&lt;string&gt;(selectedCourses);
          var instructorCourses = new HashSet&lt;int&gt;
              (instructorToUpdate.Courses.Select(c =&gt; c.CourseID));
          foreach (var course in db.Courses)
          {
              if (selectedCoursesHS.Contains(course.CourseID.ToString()))
              {
                  if (!instructorCourses.Contains(course.CourseID))
                  {
                      instructorToUpdate.Courses.Add(course);
                  }
              }
              else
              {
                  if (instructorCourses.Contains(course.CourseID))
                  {
                      instructorToUpdate.Courses.Remove(course);
                  }
              }
          }
      }
  
      private void PopulateAssignedCourseData(Instructor instructor)
      {
          var allCourses = db.Courses;
          var instructorCourses = new HashSet&lt;int&gt;(instructor.Courses.Select(c =&gt; c.CourseID));
          var viewModel = new List&lt;AssignedCourseData&gt;();
          foreach (var course in allCourses)
          {
              viewModel.Add(new AssignedCourseData
              {
                  CourseID = course.CourseID,
                  Title = course.Title,
                  Assigned = instructorCourses.Contains(course.CourseID)
              });
          }
          ViewBag.Courses = viewModel;
      }
  </code></pre>
  
  <p>edit.cshtml</p>
  
  <pre><code>    int cnt = 0;
      List&lt;UniversitySystem.ViewModels.AssignedCourseData&gt; courses = ViewBag.Courses;
  
      foreach (var course in courses)
      {
          if (cnt++ % 3 == 0)
          {
                          @:  &lt;/tr&gt; &lt;tr&gt; 
                      }
                      @: &lt;td&gt; 
                          &lt;input type="checkbox" 
                                 name="selectedCourses" 
                                 value="@course.CourseID" 
                                 @(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")) /&gt; 
                          @course.CourseID @:  @course.Title
                      @:&lt;/td&gt;
                  }
                  @: &lt;/tr&gt;
              }
      &lt;/table&gt;
  &lt;/div&gt;
          &lt;p&gt;
              &lt;input type="submit" value="Save" /&gt;
          &lt;/p&gt;
      &lt;/fieldset&gt;
  }
  </code></pre>
  
  <p>ViewModels/AssignedCourseData.cs</p>
  
  <pre><code>    public class AssignedCourseData
      {
          public int CourseID { get; set; }
          public string Title { get; set; }
          public bool Assigned { get; set; }
      }
  }
  </code></pre>
  
  <p>A great deal of code to effectivly create this screen:
  <img src="https://i.stack.imgur.com/d781c.png" alt="Screen"></p>
  
  <p>I guess you could generalise the helper methods used in the InstructorController, but that is no small task.</p>
  
  <p>It seems such a fundamental component of CRUD systems to deal with one/many to many relationships; I am surprised I cannot find information on the topic.</p>
  
  <p>TLDR: Is there a better way to associate objects to other objects using MVC3/Entity framework than what is shown above.</p>
  
  <p>Edit2:
  Here's an image of a quick Lightswitch application
  <img src="https://i.stack.imgur.com/f4J9e.png" alt="enter image description here">
  A Candidate can have a number of Skills, Disabilities and Offences related to them. If i were to implement a MVC version of this system i would x3 of the code listed above to create the same effect.</p>
  
  <p>Surely there is a more efficient solution.</p>