title: Updating a list of objects in mvc4
body: <p>I have taken an issue that I am having with a asp.net mvc4 applications and simplified it so I can post it here. My basic problem is that I am trying to send a list of items to my view and edit the check box of any of the items and then send the list items back to the controller and be able to eventually save to the database. The way the code is written right now when I send the list back to the controller it comes across as a null value just like it was never instantiated.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>Public Class Person
      Property ID As Integer
      Property Name As String
      Property Active As Boolean
  End Class
  </code></pre>
  
  <p>In the controller I call a class called BuildPeople that is really just a way to build the the list to pass around:</p>
  
  <pre><code>Public Class BuildPeople
  
      Public Function GetPersonList() As List(Of Person)
          Dim personList As New List(Of Person)
          personList.Add(GetPerson(1, "Chris", True))
          personList.Add(GetPerson(2, "Ken", True))
          personList.Add(GetPerson(3, "Jen", True))
          Return personList
      End Function
  
      Private Function GetPerson(id As Integer, name As String, active As Boolean) As Person
          Dim p As New Person
          p.ID = id
          p.Name = name
          p.Active = active
          Return p
      End Function  
  End Class
  </code></pre>
  
  <p>The controller only has the edit ability:</p>
  
  <pre><code>    Imports System.Web.Mvc
  
  Public Class PeopleController
      Inherits Controller
  
      ' GET: /People
      Function Index() As ActionResult
          Return View()
      End Function
  
      ' GET: /People/Edit/5
      Function Edit() As ActionResult
          Dim bp As New BuildPeople
          Dim model As New List(Of Person)
          model = bp.GetPersonList
          ViewData.Model = model
          Return View()
      End Function
  
      ' POST: /People/Edit/5
      &lt;HttpPost()&gt;
      Function Edit(ByVal listPeople As List(Of Person)) As ActionResult
          Try
              ' TODO: Add update logic here
              If listPeople Is Nothing Then
                  'Don't want to end up here
                  Return View()
              Else
                  'Want to end up here
                  Dim i As Integer = listPeople.Count
                  Return View()
              End If
          Catch
              Return View()
          End Try
      End Function
  End Class
  </code></pre>
  
  <p>And then the view is as follows:</p>
  
  <pre><code>@ModelType List(Of Person)
  @Code
      ViewData("Title") = "Edit"
      Layout = "~/Views/Shared/_Layout.vbhtml"
  End Code
  
  &lt;h2&gt;Edit&lt;/h2&gt;
  
  @Using (Html.BeginForm())
      @Html.AntiForgeryToken()
  
      @&lt;div class="form-horizontal"&gt;
          &lt;h4&gt;Person&lt;/h4&gt;
          &lt;hr /&gt;
          @For Each item In Model
          Dim currentitem = item
              @Html.HiddenFor(Function(model) currentitem.ID)
              @Html.EditorFor(Function(model) currentitem.Name)
              @Html.EditorFor(Function(model) currentitem.Active)
          Next
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Save" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  End Using
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  </code></pre>