C# 在asp mvc中访问特定索引
假设我有这个C# 在asp mvc中访问特定索引,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,假设我有这个 @using MvcApplication6.Models; @model List<Employee> 我只想显示页面中的特定记录。我应该怎么做才能达到这个结果 完整代码 @using MvcApplication6.Models; @model IEnumerable<Employee> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @
@using MvcApplication6.Models;
@model List<Employee>
我只想显示页面中的特定记录。我应该怎么做才能达到这个结果
完整代码
@using MvcApplication6.Models;
@model IEnumerable<Employee>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New record", "Create")
</p>
@Html.LabelFor(model => model.Model[0].firstname)
<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", "Employee", new { id=item.id },null) |
@Html.ActionLink("Delete", "Delete", new { id=item.id }) |
@Html.DisplayFor(modelItem => item.Players[0])
</td>
</tr>
}
</table>
@使用mvcapapplication6.Models;
@模型IEnumerable
@{
ViewBag.Title=“Index”;
}
指数
@ActionLink(“创建新记录”、“创建”)
@Html.LabelFor(model=>model.model[0].firstname)
@ActionLink(“检查部门”、“索引”、“部门”)
@DisplayNameFor(model=>model.id)
@DisplayNameFor(model=>model.firstname)
@DisplayNameFor(model=>model.lastname)
@DisplayNameFor(model=>model.gender)
@DisplayNameFor(model=>model.department)
行动
@foreach(模型中的员工项目){
@DisplayFor(modeleItem=>item.id)
@DisplayFor(modelItem=>item.firstname)
@DisplayFor(modelItem=>item.lastname)
@DisplayFor(modeleItem=>item.gender)
@DisplayFor(modelItem=>item.department)
@ActionLink(“编辑”,“编辑”,新的{id=item.id})|
@ActionLink(“详细信息”、“详细信息”、“员工”,新的{id=item.id},null)|
@ActionLink(“删除”,“删除”,新的{id=item.id})|
@Html.DisplayFor(modeleItem=>item.Players[0])
}
您正在尝试访问集合实例上的属性模型。相反,只需通过索引引用模型即可访问firstname属性:
@Html.LabelFor(m => m[0].firstname)
lambda表达式公开模型,在您的示例中,该模型是一个集合。此外,您应该将@model解析为
List
而不是IEnumerable
,以防止延迟计算。您确定列表中确实有记录吗?是的,因为我通过表显示它们。@Html.LabelFor(model=>model[0].firstname)
如果模型至少包含一个项目,则不会创建错误。但是,您的代码段有@Html.LabelFor(model=>model.model[0].firstname)
,这是因为Employee
没有名为model
的属性。不要使用@Html.LabelFor(model=>model[0].firstname)
(大写M)根据您接受的答案(在某些情况下会导致错误)@DavidL,我认为真正的问题是“完整代码”片段有@Html.LabelFor(model=>model.model[0].firstname)
(并且员工的类型
没有名为模型
)的属性,并且该OP实际上没有尝试@Html.LabelFor(Model=>Model[0]。firstname)
尽管有这样的说法。但问题还是从它的@model List
开始,但后面的代码显示@model IEnumerable
,所以谁知道呢:)我尽了最大努力解决了本例中所有可能出现的问题:)。我实际上怀疑这个问题是IEnumerable,再加上糟糕的语法。谢谢你指出错误。
@Html.LabelFor(m => m[0].firstname)