简体   繁体   中英

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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