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
Therefor:
The view model
public class vm_PersonList { public Person Person { get; set; } public int age { get; set; } }The controller action:
public ActionResult PersonList() { ViewBag.Message = "My List"; var list = new List<vm_PersonList>(); 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); }The view
@model List<programname.Viewmodels.vm_PersonList> @foreach (var p in Model) { <tr> <td> @p.Person.FullName </td> <td> @p.age </td> </tr> }
The Person table contains for example 6 entries. When debugging the application I see:
At the end of the controller action "list" contains correctly the 6 different Person entries
In the view, the "Model" contains 6 entries, but 6 times the last "database entry". Does anyone have a suggestion to solve this issue?