如何在运行时从强类型列表脚手架视图的 Display(Name=) DataAnnotation 中获取列标题? [英] How to get the column titles from the Display(Name=) DataAnnotation for a strongly typed list scaffold view at runtime?

查看:12
本文介绍了如何在运行时从强类型列表脚手架视图的 Display(Name=) DataAnnotation 中获取列标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获得在 List scaffold 视图输出中呈现的 [Display(Name="Some Title")] DataAnnotationsSome Title"?

How do I get the [Display(Name="Some Title")] DataAnnotations "Some Title" rendered in the List scaffold view's output?

我为这个类创建了一个强类型列表脚手架视图:

I create a strongly typed list scaffold view for this class:

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

However, I don't want "Date" and "Name" in the table header. I want "Datum" and "Feiertag Name" rendered there dynamically.

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

The actual column header titles should come from the Display DataAnnotation.

我该怎么做?

推荐答案

有点老话题,但我想出了一个扩展方法来处理这个问题.

Bit of an old topic, but I came up with an extension method to handle this.

假设我的模型是:

public class MyModel
{
    [Display(Name = "Some Property")]
    public string SomeProp { get; set; }
}

将此方法放入静态类:

namespace Web.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString DisplayNameFor<TModel, TProperty>(this HtmlHelper<IEnumerable<TModel>> helper, Expression<Func<TModel, TProperty>> expression)
        {
            var name = ExpressionHelper.GetExpressionText(expression);
            name = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(() => Activator.CreateInstance<TModel>(), typeof(TModel), name);
            return new MvcHtmlString(metadata.DisplayName);
        }
    } 
}

然后在您的视图中,它采用 IEnumerable,您可以像这样使用它:

Then in your View, which is taking an IEnumerable<MyModel>, you can use it like this:

@using Web.Extensions
@model IEnumerable<MyModel>

<table>
    <tr>
        <th>
            @Html.DisplayNameFor(m => m.AvgElecCost)
        </th>

生成的 HTML 将是:

The generated HTML will be:

        <th>
            Some Property
        </th>

希望能帮到你!

这篇关于如何在运行时从强类型列表脚手架视图的 Display(Name=) DataAnnotation 中获取列标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆