title: ASP MVC4 - Pass List to view via view model
body: <p>I have a model Person (with among other fields the day of Birth)
  and I want to pass a list of all persons, together with the calculated age of each person, to the view </p>
  
  <p>Therefor:</p>
  
  <ol>
  <li><p>The view model</p>
  
  <pre><code>public class vm_PersonList
  {
      public Person Person { get; set; } 
      public int age { get; set; }
  }
  </code></pre></li>
  <li><p>The controller action:</p>
  
  <pre><code>public ActionResult PersonList()
  {
      ViewBag.Message = "My List";
  
      var list = new List&lt;vm_PersonList&gt;();
      var list_p = new vm_PersonList();
  
  
      foreach (var p in db.Person)
      {
          list_p.Person = p;
          //the age will be calculated based on p.birthDay, not relevant for the    
          //current question
          list_p.age = 23;
  
          list.Add(list_p);
      }
      return View(list);
  }
  </code></pre></li>
  <li><p>The view</p>
  
  <pre><code>@model List&lt;programname.Viewmodels.vm_PersonList&gt;
  
  @foreach (var p in Model)
  {
      &lt;tr&gt;
          &lt;td&gt;
              @p.Person.FullName
          &lt;/td&gt;
          &lt;td&gt;
              @p.age
          &lt;/td&gt;  
      &lt;/tr&gt;
  }
  </code></pre></li>
  </ol>
  
  <p>The Person table contains for example 6 entries.
  When debugging the application I see:</p>
  
  <p>At the end of the controller action "list" contains correctly the 6 different Person entries</p>
  
  <p>In the view, the "Model" contains 6 entries, but 6 times the last "database entry".
  Does anyone have a suggestion to solve this issue?</p>