我可以在gridview中为数据输入和列标题设置一个DisplayName吗?

时间:2013-12-11 22:40:54

标签: c# asp.net-mvc data-annotations webgrid

在显示数据时,有没有办法在标题中使用[DisplayName("My descriptive Name")]而只使用列名?

(无需键入每个?) - (我使用MVC脚手架创建编辑页面)

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

1 个答案:

答案 0 :(得分:0)

添加属性在此模型中显示

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

在Razor View中,使用

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

希望这能帮助你。