Search code examples
c#asp.net-mvcdata-annotationswebgrid

Can I have one DisplayName for data entry and the Column Headers in a gridview?


Is there a way to use the [DisplayName("My descriptive Name")] in headers and just the column name when I display data?

(Without having to type each one in?) - (I used MVC scaffolding to create the edit page)

<p>
    @Html.ActionLink("Create New", "CreateTask")
</p>
<table>
    <tr>
    <th>
        @Html.DisplayNameFor(model => model.Name)
    </th>
    </tr>
<td>
@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
</tr>
}
</table>

Solution

  • Add properties Display in Model like this

    [Display(Name = "Your Custom Name")]
     public string Name { get; set; }
    

    And in Razor View, use

    @Html.LabelFor(model=>model.Name)
    

    Hope this help you DAN.