如何设置页面';的标题来自asp.net中的模型

本文关键字:标题 asp 模型 net 何设置 设置 | 更新日期: 2023-09-27 17:59:06

这是我的视图代码。

@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这样的东西。然而,这样做,我得到了一个错误。那么语法是什么呢?

如何设置页面';的标题来自asp.net中的模型

问题是您的Model类型为IEnumerable<TestApplication.Models.ApplicationUser>,因此您不能调用ApplicationUser 的Property

要证明问题,请尝试以下操作:

ViewBag.Title = Model.First().FirstName;

这个错误应该会消失,但这不是正确的解决方案。您应该考虑要显示

的用户列表中哪个的名字