如何在WebGrid中为列标题使用DisplayName数据注释?

时间:2021-10-05 17:03:26

I have a Car class that I'm trying to display in an MVC 3 view using the WebGrid helper. Below are the Car and it's metadata class.

我有一个Car类,我试图使用WebGrid助手在MVC 3视图中显示。下面是Car及其元数据类。

Car class:

汽车类:

[MetadataType(typeof(CarMetadata))]
public partial class Car
{
    // car implementation
}

Car metadata class:

汽车元数据类:

public class CarMetadata
{        
    [DisplayName("Car Name")]
    [StringLength(100, ErrorMessageResourceType = typeof(ValidationText), ErrorMessageResourceName="CarNameDescriptionLength")]
    [Required]
    public string CarName { get; set; }    
}

View contents:

查看内容:

@model List<Car>
...
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.NextPrevious);

@grid.GetHtml(
    htmlAttributes: new { id = "grid" },
    columns: grid.Columns(
        grid.Column("CarName", ?????)
    ));

GOAL: I'd like to figure out how to use the DisplayName data annotation as the column header text in the WebGrid (?????). Does anyone know how this is accomplished?

目标:我想弄清楚如何使用DisplayName数据注释作为WebGrid(?????)中的列标题文本。有谁知道这是如何实现的?

Thanks in advance!

提前致谢!

2 个解决方案

#1


10  

Ugly as hell but it could work:

丑陋,但它可以工作:

grid.Column(
    "CarName", 
    ModelMetadata.FromLambdaExpression(
        car => car.CarName, 
        new ViewDataDictionary<Car>(new Car())
    ).DisplayName
)

The problem is that the WebGrid helper is entirely based on dynamic data, absolutely no strong typing and that's one of the reasons why I hate it. The WebMatrix team at Microsoft must be real fans of the C# 4.0 dynamic feature as their entire API takes only weakly typed objects :-)

问题是WebGrid助手完全基于动态数据,绝对没有强类型,这也是我讨厌它的原因之一。 Microsoft的WebMatrix团队必须是C#4.0动态功能的真正粉丝,因为他们的整个API只接受弱类型对象:-)

MvcContrib Grid is much better.

MvcContrib Grid要好得多。

#2


0  

I have created a helper method like this:

我创建了一个这样的辅助方法:

public static string GetDisplayName<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> property)
{
    return GetDisplay(property);
}

public static string GetDisplayName<TModel, TProperty>(this HtmlHelper<IEnumerable<TModel>> html, Expression<Func<TModel, TProperty>> property)
{
    return GetDisplay(property);
}

private static string GetDisplay<M,P>(Expression<Func<M,P>> property)
{
    var propertyExp = (MemberExpression)property.Body;
    var member = propertyExp.Member;
    var disp = (DisplayAttribute)member.GetCustomAttribute(typeof(DisplayAttribute));
    if (disp == null)
    {
        return member.Name;
    }
    return disp.Name;
}

And used it like this:

并像这样使用它:

new WebGridColumn { Header = Html.GetDisplayName(t=>t.Title), ColumnName = nameof(DataModel.Title), CanSort=true }

#1


10  

Ugly as hell but it could work:

丑陋,但它可以工作:

grid.Column(
    "CarName", 
    ModelMetadata.FromLambdaExpression(
        car => car.CarName, 
        new ViewDataDictionary<Car>(new Car())
    ).DisplayName
)

The problem is that the WebGrid helper is entirely based on dynamic data, absolutely no strong typing and that's one of the reasons why I hate it. The WebMatrix team at Microsoft must be real fans of the C# 4.0 dynamic feature as their entire API takes only weakly typed objects :-)

问题是WebGrid助手完全基于动态数据,绝对没有强类型,这也是我讨厌它的原因之一。 Microsoft的WebMatrix团队必须是C#4.0动态功能的真正粉丝,因为他们的整个API只接受弱类型对象:-)

MvcContrib Grid is much better.

MvcContrib Grid要好得多。

#2


0  

I have created a helper method like this:

我创建了一个这样的辅助方法:

public static string GetDisplayName<TModel, TProperty>(this HtmlHelper<TModel> html, Expression<Func<TModel, TProperty>> property)
{
    return GetDisplay(property);
}

public static string GetDisplayName<TModel, TProperty>(this HtmlHelper<IEnumerable<TModel>> html, Expression<Func<TModel, TProperty>> property)
{
    return GetDisplay(property);
}

private static string GetDisplay<M,P>(Expression<Func<M,P>> property)
{
    var propertyExp = (MemberExpression)property.Body;
    var member = propertyExp.Member;
    var disp = (DisplayAttribute)member.GetCustomAttribute(typeof(DisplayAttribute));
    if (disp == null)
    {
        return member.Name;
    }
    return disp.Name;
}

And used it like this:

并像这样使用它:

new WebGridColumn { Header = Html.GetDisplayName(t=>t.Title), ColumnName = nameof(DataModel.Title), CanSort=true }