如何更改@Html.DisplayNameFor(model => model.name) 的显示

0 c# asp.net-mvc

我做了一个 DataBase Fisrt MVC 项目,但我遇到了一些问题。这是我的一个观点的索引:例如,我想要

@Html.DisplayNameFor(model => model.name)

显示为“用户”而不是“姓名”。

我怎样才能做到这一点?

提前致谢。

@model IEnumerable<dispensario.pacientes>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.cedula)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.carnet)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.tipo_paciente)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.estado)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.cedula)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.carnet)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.tipo_paciente)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.estado)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.idPaciente }) |
            @Html.ActionLink("Details", "Details", new { id=item.idPaciente }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.idPaciente })
        </td>
    </tr>
}

</table>
Run Code Online (Sandbox Code Playgroud)

Jai*_*ave 7

您可以将 DisplayName 属性添加到您的视图模型。

[DisplayName("User")]
public string name { get; set; }
Run Code Online (Sandbox Code Playgroud)

在您看来:@Html.DisplayNameFor(model => model.name)

或者你可以只写“用户”来查看而不是 html 助手。

即在您看来:

<th>User</th>
Run Code Online (Sandbox Code Playgroud)