首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在运行时从强类型列表脚手架视图的Display(Name=) DataAnnotation获取列标题?

如何在运行时从强类型列表脚手架视图的Display(Name=) DataAnnotation获取列标题?
EN

Stack Overflow用户
提问于 2011-02-10 00:44:15
回答 3查看 32.2K关注 0票数 17

如何在列表脚手架视图的输出中呈现某个标题“[Display(Name="Some Title")] DataAnnotations”?

我为这个类创建了一个强类型的list scaffold视图:

public class CompanyHoliday
{
    [Key]
    public int Id { get; set; }

    [Required]
    [Display(Name = "Datum")]
    [DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
    public DateTime Date { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name = "Feiertag Name")]
    public string Name { get; set; }
}

视图如下所示:

@model IEnumerable<Zeiterfassung.Domain.CompanyHoliday>

@{
    ViewBag.Title = "Year";
}

<h2>Year</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
    <th></th>
    <th>
        Date
    </th>
    <th>
        Name
    </th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
    <td>
        @String.Format("{0:g}", item.Date)
    </td>
    <td>
        @item.Name
    </td>
</tr>
}

</table>

但是,我不想在表头中出现“日期”和“名称”。我希望"Datum“和"Feiertag Name”在那里动态呈现。

实际的列标题标题应该来自Display DataAnnotation。

我该怎么做呢?

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4947854

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档