How can I use DisplayName data annotations for column headers in WebGrid?
Asked Answered
T

2

14

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

Thanks in advance!

Textbook answered 9/3, 2011 at 18:55 Comment(0)
B
11

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

MvcContrib Grid is much better.

Bender answered 9/3, 2011 at 19:4 Comment(4)
Thanks for the response. Does MvcContrib work with the DataAnnotations?Textbook
@80bower, yes it is strongly typed and you can easily achieve this.Bender
Thanks for the input. I'm going to look into using MvcContrib. Yes, your solution worked, and yes, it was ugly. :)Textbook
unfortunately, MvcContrib Grid isn't really maintained any more (as the last update is from 2011)Borchert
S
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 }
Stargell answered 19/1, 2018 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.