title: View Models and Check Boxes in View
body: <p>I have a newbie question, which I have tried to understand for the past few days. Hopefully someone can be kind enough to help me understand the programming flow.</p>
  
  <p>Assuming I have a model, the information is stored in the database:</p>
  
  <pre><code>public class Student
  {
      public int studentID { get; set; }
      public string studentName { get; set; }
      public strin studentGrade {get; set; }
  }
  
  public class StudentDBContext : DbContext
  {
      public DbSet&lt;Student&gt; Students { get; set; }
  }
  </code></pre>
  
  <p>and I want to display it into the view, with additional checkbox so I can select which students to be promoted into the next grade. I read that one way to do it is by putting into view model:</p>
  
  <pre><code>public class StudentViewModel
  {
      public bool promoted { get; set; }
      public Student stu { get; set; }
  }
  </code></pre>
  
  <p>But I am stuck on is this the way to do it? and if yes, how do you put into the view where it will display all the students with a checkbox next to it. Afterwards, I want to update all the grade for the students whose checkboxes are ticked. For example:</p>
  
  <p>Student A, Student B, Student D promoted from Grade 1 to Grade 2. So I want to display the students, tick Student A, B and D and submit to update the Grade.</p>
  
  <p>Step by step example will be much appreciated.</p>
  
  <p>Update 1:</p>
  
  <p>Controller:</p>
  
  <pre><code>    [HttpGet]
      public ViewResult CheckBox()
      {
          var studentViewModels = db.Students.Select(m =&gt; new StudentViewModel()
          {
              stu = m
          }).ToList();
  
          return View(studentViewModels);
      }
  
      [HttpPost]
      public ActionResult CheckBox(IList&lt;studentViewModel&gt; list)
      {
  
          foreach (var stuUpdate in list.Where(m =&gt; m.promoted))
          {
              var stuRow = db.Students.Find(stuUpdate.stu.studentID);
              stuRow.studentName = stuRow.studentName + "1";
              db.Entry(stuRow).State = EntityState.Modified;
              db.SaveChanges();
              return RedirectToAction("CheckBox");
          }
          return RedirectToAction("CheckBox");
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@model IList&lt;School.ViewModels.StudentViewModel&gt;
  
  @using (Html.BeginForm())
  {
  &lt;table&gt;
  &lt;tr&gt;
      &lt;th&gt;
  
      &lt;/th&gt;
      &lt;th&gt;
          student ID
      &lt;/th&gt;
      &lt;th&gt;
          student name
      &lt;/th&gt;
      &lt;th&gt;
          student grade
      &lt;/th&gt;
  &lt;/tr&gt;
  @foreach (var item in Model) {
      &lt;tr&gt;
          &lt;td&gt;
              @Html.CheckBoxFor(modelItem =&gt; item.promoted)
              @Html.HiddenFor(modelItem =&gt; item.stu.studentID)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.stu.studentID)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.stu.studentName)
          &lt;/td&gt;
          &lt;td&gt;
              @Html.DisplayFor(modelItem =&gt; item.stu.studentGrade)
          &lt;/td&gt;
      &lt;/tr&gt;
  }
  
  &lt;/table&gt;
  &lt;input type="submit" value="save" /&gt;
  }
  </code></pre>
  
  <p>However currently hit by the following error:
      Value cannot be null.
      Parameter name: source</p>
  
  <pre><code>Source Error:
  foreach (var stuUpdate in list.Where(m =&gt; m.promoted))
  </code></pre>