简体   繁体   English

ASP MVC4 - 通过视图模型查看通过列表

[英]ASP MVC4 - Pass List to view via view model

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: 为此:

  1. The view model 视图模型

     public class vm_PersonList { public Person Person { get; set; } public int age { get; set; } } 
  2. 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); } 
  3. 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. Person表包含例如6个条目。 When debugging the application I see: 在调试应用程序时,我看到:

At the end of the controller action "list" contains correctly the 6 different Person entries 在控制器操作结束时,“list”正确包含6个不同的Person条目

In the view, the "Model" contains 6 entries, but 6 times the last "database entry". 在视图中,“模型”包含6个条目,但是最后一个“数据库条目”的6倍。 Does anyone have a suggestion to solve this issue? 有没有人有解决这个问题的建议?

You are using the same list_p instance over and over again inside the loop. 您在循环内list_p使用相同的list_p实例。 So you are constantly updating its Person property. 所以你不断更新其Person属性。 And since Person is a reference type you are modifying the same reference in memory. 由于Person是一个引用类型,因此您在内存中修改相同的引用。 At the last iteration of the loop you are obviously replacing this reference with the last instance of Person which explains why you are seeing the same person in the view. 在循环的最后一次迭代中,你显然用最后一个Person实例替换了这个引用,这解释了为什么你在视图中看到同一个人。

Try like this, seems lot easier: 尝试这样,似乎很容易:

public ActionResult PersonList()
{
    ViewBag.Message = "My List";
    var model = db.Person.Select(p => new vm_PersonList
    {
        Person = p,
        age = 23
    }).ToList();
    return View(model);
}

You are working on the same instance of vm_PersonList. 您正在处理相同的vm_PersonList实例。 Move the instantiation of vm_PersonList into the loop 将vm_PersonList的实例化移动到循环中

foreach (var p in db.Person)
{
    var list_p = new vm_PersonList();
    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);
}

It's an issue with the scope of your list_p instance. 这是list_p实例范围的问题。 Try changing your controller code to: 尝试将控制器代码更改为:

public ActionResult PersonList()
{
    ViewBag.Message = "My List";

    var list = db.Person
        .Select(p => new vm_PersonList
                     {
                         Person = p,
                         age = 23,
                     })
        .ToList();

    return View(list);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM