这是我的观看代码。
@model IEnumerable<TestApplication.Models.ApplicationUser>
@{
Layout = null;
ViewBag.Title = //what goes here? Model.FirstName?
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
</head>
<body>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.FirstName)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.FirstName)
</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 })
</td>
</tr>
}
</table>
</body>
</html>
我想将ViewBag的标题更改为模型中FirstName的名称。我期待ViewBag.Title = Model.FirstName
之类的东西。但是这样做,我收到了一个错误。那么语法是什么?
答案 0 :(得分:0)
问题是您的模型属于IEnumerable<TestApplication.Models.ApplicationUser>
类型,因此您无法调用ApplicationUser
要演示此问题,请尝试以下操作:
ViewBag.Title = Model.First().FirstName;
错误应该消失,但这不是正确的解决方案。您应该从您想要显示的用户列表中考虑哪个名字