C# 在asp mvc中将列表传递给aview:list vs IEnumerable
只是一个简单的问题C# 在asp mvc中将列表传递给aview:list vs IEnumerable,c#,asp.net,C#,Asp.net,只是一个简单的问题 @using MvcApplication6.Models; @model IEnumerable<Employee> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New record", "Create") </p> <p>@Html.ActionLink("Check dep
@using MvcApplication6.Models;
@model IEnumerable<Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New record", "Create")
</p>
<p>@Html.ActionLink("Check departments", "Index", "Department")</p>
<table border="1">
<tr>
<th>
@Html.DisplayNameFor(model => model.id)
</th>
<th>
@Html.DisplayNameFor(model => model.firstname)
</th>
<th>
@Html.DisplayNameFor(model => model.lastname)
</th>
<th>
@Html.DisplayNameFor(model => model.gender)
</th>
<th>
@Html.DisplayNameFor(model => model.department)
</th>
<th>Action</th>
</tr>
@foreach (Employee item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.firstname)
</td>
<td>
@Html.DisplayFor(modelItem => item.lastname)
</td>
<td>
@Html.DisplayFor(modelItem => item.gender)
</td>
<td>
@Html.DisplayFor(modelItem => item.department)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.id }) |
@Html.ActionLink("Details", "Details", new { id=item.id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.id }) |
@Html.DisplayFor(modelItem => item.Players[0])
</td>
</tr>
}
</table>
我在其他视图上测试它,它被声明为一个列表:
@model List<MvcApplication6.Models.Employee>
@型号列表
尽管不涉及
DisplayName for
和DisplayFor
,但它仍能正常工作。IEnumerable当您只需要迭代时,可以使用
IEnumerable
(完整列表请参见此处:),这意味着如果您只想对列表执行foreach
,则应返回一个IEnumerable
列表您可以使用
List
获取需要迭代、修改、排序等的对象列表(完整列表请参见此处) 错误肯定会有助于了解问题的原因,而且我怀疑@Html.DisplayNameFor(model=>model.id)
可能在计算所有类型时遇到问题,因为model不完全是IEnumerable
。
@Html.DisplayNameFor(model => model.id)
@model List<MvcApplication6.Models.Employee>