title: How to know the selected checkboxes from within the HttpPost Create action method?
body: <p>I have many-to-many relationship between <code>Student</code> and <code>Course</code>. The linking entity set is <code>Enrollment</code>. For the sake of simplicity, they are all defined as follows.</p>
  
  <h1>Models</h1>
  
  <pre><code>public class Course
  {
      public int Id { get; set; }
      public string Title { get; set; }
  
      public virtual ICollection&lt;Enrollment&gt; Enrollments { get; set; }
  }
  
  public class Enrollment
  {
      public int Id { get; set; }
      public int StudentId { get; set; }
      public int CourseId { get; set; }
  
      public virtual Student Student { get; set; }
      public virtual Course Course { get; set; }
  }
  
  public class Student
  {
      public int Id { get; set; }
      public string Name { get; set; }
  
      public virtual ICollection&lt;Enrollment&gt; Enrollments { get; set; }
  }
  </code></pre>
  
  <h1>ViewModels</h1>
  
  <pre><code>public class StudentCourseVM
  {
      public Student Student { get; set; }
      public IEnumerable&lt;Course&gt; SelectedCourses { get; set; }
      public IEnumerable&lt;Course&gt; AvailableCourses { get; set; }
  }
  </code></pre>
  
  <h1>Controllers</h1>
  
  <pre><code>    public IActionResult Create()
      {
          var availableCourses = context.Courses;
          return View(new StudentCourseVM { AvailableCourses = availableCourses });
      }
  
  
      [HttpPost]
      public async Task&lt;IActionResult&gt; Create(StudentCourseVM sc)
      {
          if (ModelState.IsValid)
          {
              // What should I do here?
              // ======================
              await context.SaveChangesAsync();
              return RedirectToAction("Index");
          }
          return View(sc);
      }
  </code></pre>
  
  <h1>Views</h1>
  
  <pre><code>@model MasterDetails.ViewModels.StudentCourseVM
  &lt;form asp-action="Create"&gt;
      &lt;div&gt;
          &lt;label asp-for="@Model.Student.Name"&gt;&lt;/label&gt;
          &lt;input asp-for="@Model.Student.Name" /&gt;
      &lt;/div&gt;
      &lt;div&gt;
          &lt;label asp-for="@Model.Student.Enrollments"&gt;&lt;/label&gt;&lt;br /&gt;
          @foreach (var course in Model.AvailableCourses)
          {
              &lt;input type="checkbox" name="@course.Title" id="@course.Id" /&gt; @course.Title &lt;br /&gt;
          }
      &lt;/div&gt;
      &lt;input type="submit" value="Create" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <h1>Questions</h1>
  
  <p>How to know the selected check boxes from within the HttpPost <code>Create</code> action method?</p>