C# ASP MVC4 - 传递列表以通过视图模型查看
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10750718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP MVC4 - Pass List to view via view model
提问by user1416778
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> }
视图模型
public class vm_PersonList { public Person Person { get; set; } public int age { get; set; } }控制器动作:
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); }风景
@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:
Person 表包含例如 6 个条目。调试应用程序时,我看到:
At the end of the controller action "list" contains correctly the 6 different Person entries
在控制器操作“列表”的末尾正确包含 6 个不同的人员条目
In the view, the "Model" contains 6 entries, but 6 times the last "database entry". Does anyone have a suggestion to solve this issue?
在视图中,“模型”包含 6 个条目,但是最后一个“数据库条目”的 6 倍。有没有人有解决这个问题的建议?
回答by Darin Dimitrov
You are using the same list_pinstance over and over again inside the loop. So you are constantly updating its Person property. And since Personis a reference type you are modifying the same reference in memory. 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.
您list_p在循环中一遍又一遍地使用相同的实例。所以你会不断更新它的 Person 属性。由于Person是引用类型,您正在修改内存中的相同引用。在循环的最后一次迭代中,您显然将这个引用替换为 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);
}
回答by terjetyl
You are working on the same instance of vm_PersonList. Move the instantiation of vm_PersonList into the loop
您正在处理 vm_PersonList 的同一个实例。将 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);
}
回答by mattytommo
It's an issue with the scope of your list_pinstance. Try changing your controller code to:
这是您的list_p实例范围的问题。尝试将您的控制器代码更改为:
public ActionResult PersonList()
{
ViewBag.Message = "My List";
var list = db.Person
.Select(p => new vm_PersonList
{
Person = p,
age = 23,
})
.ToList();
return View(list);
}

