I have a Person model (with other birthday fields) and I want to transfer a list of all persons along with the calculated age of each person to the presentation
Therefor:
Presentation model
public class vm_PersonList { public Person Person { get; set; } public int age { get; set; } }
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;
View
@model List<programname.Viewmodels.vm_PersonList> @foreach (var p in Model) { <tr> <td> @p.Person.FullName </td> <td> @p.age </td> </tr> }
The table Person contains, for example, 6 records. When debugging the application, I see:
At the end of the list action, the controller correctly contains 6 different Person entries
In the view, the "Model" contains 6 records, but 6 times the last "record in the database." Anyone have a suggestion to solve this problem?
source share