简体   繁体   English

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

[英]How to change the display of @Html.DisplayNameFor(model => model.name)

I made a DataBase Fisrt MVC project and I'm having some issues with this. 我做了一个Database Fisrt MVC项目,与此有关。 This is the Index of one of my Views: And for example, I would like 这是我的一种观点的索引:例如,我想

@Html.DisplayNameFor(model => model.name) @ Html.DisplayNameFor(模型=>模型。名称)

to be displayed as "User" instead of "name". 显示为“用户”而不是“名称”。

How can I do this? 我怎样才能做到这一点?

Thanks in advance. 提前致谢。

@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>

You can add DisplayName attribute to your viewmodel. 您可以将DisplayName属性添加到您的视图模型。

[DisplayName("User")]
public string name { get; set; }

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

Or you can just write "User" to view instead of html helper. 或者,您可以只写“用户”来查看,而不是html helper。

ie in your view: 即在您看来:

<th>User</th>

Add DisplayName Attribute to your property to change the display name in your model 将DisplayName属性添加到属性中以更改模型中的显示名称

[DisplayName("User")]
public string name { get; set; }

I had the same "problem" and to properly use the Data Annotation [DisplayName("User")] in Visual Studio Code I had to add the using System.ComponentModel; 我遇到了同样的“问题”,要在Visual Studio Code中正确使用数据注释[DisplayName(“ User”)] ,必须添加using System.ComponentModel。 to my model file. 到我的模型文件。 Without this using I could not compile successfully. 没有此使用,我将无法成功编译。 Hope this tip could help someone 希望这个技巧可以帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM