Miguel
Miguel

Reputation: 13

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. This is the Index of one of my Views: And for example, I would like

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

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>

Upvotes: 0

Views: 15837

Answers (3)

Fabio
Fabio

Reputation: 251

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; to my model file. Without this using I could not compile successfully. Hope this tip could help someone

Upvotes: 2

Jaimin Dave
Jaimin Dave

Reputation: 1222

You can add DisplayName attribute to your viewmodel.

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

In your view: @Html.DisplayNameFor(model => model.name)

Or you can just write "User" to view instead of html helper.

i.e. in your view:

<th>User</th>

Upvotes: 7

Gopesh Sharma
Gopesh Sharma

Reputation: 7030

Add DisplayName Attribute to your property to change the display name in your model

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

Upvotes: 3

Related Questions