title: Saving Many to Many relationship data on MVC Create view
body: <p>I have some issues with a Many-to-many relationship saving the results of a create view.</p>
  
  <p>I want to do a create page for a new user profile that has a checklist that lets them choose courses (many to many relationship).
  My view takes the records from the <code>Courses</code> database and shows them all with checkboxes.</p>
  
  <p>Once the user posts the data, I want to update my <code>userprofile</code> model, and also the <code>courses</code> many-to-many relationship. That's the code that I have missing!</p>
  
  <p>I'm new at MVC and I've been researching but I couldn't do it yet.</p>
  
  <p>I am following this example:
  <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application" rel="noreferrer">http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application</a></p>
  
  <p>This is the model:</p>
  
  <pre><code>public class UserProfile
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public virtual ICollection&lt;Courses&gt;  usercourses { get; set; }
  }
  
  public class Courses
  {
      public int CourseID { get; set; }
      public string CourseDescripcion { get; set; }
      public virtual ICollection&lt;UserProfile&gt; UserProfiles { get; set; }
  }
  
  public class UserProfileDBContext : DbContext
  {
      public DbSet&lt;UserProfile&gt; UserProfiles { get; set; }
      public DbSet&lt;Courses&gt; usrCourses{ get; set; }
  }
  </code></pre>
  
  <p>I also added a ViewModel:</p>
  
  <pre><code>namespace Mysolution.ViewModels
  {
      public class AssignedCourseData
      {
          public int CourseID { get; set; }
          public string CourseDescription { get; set; }
          public bool Assigned { get; set; }
      }
  }
  </code></pre>
  
  <p>This is the <code>create</code> Controller that populates the courses checkboxes:</p>
  
  <pre><code>public ActionResult Create()
  {
      PopulateCoursesData();
      return View();
  }
  
  private void PopulateCoursesData()
  {
      var CoursesData = db.usrCourses;
      var viewModel = new List&lt;AssignedCourseData&gt;();
      foreach (var item in CoursesData)
      {
          viewModel.Add(new AssignedCourseData {
              CourseID = item.CourseID,
              CourseDescription  = item.CourseDescription,
              Assigned = false });
      }
      ViewBag.CoursePopulate = viewModel;
  }
  </code></pre>
  
  <p>This is the view </p>
  
  <pre><code>@{
      int cnt = 0;
      List&lt;MySolution.ViewModels.AssignedCourseData&gt; courses = ViewBag.CoursePopulate;
  
      foreach (var course in courses)
      {
          &lt;input type="checkbox" name="selectedCourse" value="@course.CourseID" /&gt; 
          @course.CourseDescription
      }
  }
  </code></pre>
  
  <p>And this is the controler that gets the data (and where I want to save it).
  It gets as a parameter <code>string[] selectedCourse</code> for the checkboxes:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Create(UserProfile userprofile, string[] selectedCourse)
  {
      if (ModelState.IsValid)
      {
          db.UserProfiles.Add(userprofile);
  
          //TO DO: Save data from many to many (this is what I can't do!)
  
          db.SaveChanges();
      }
  
      return View(userprofile);
  }
  </code></pre>