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:
[Meta
I have created a helper method like this:
public static string GetDisplayName(this HtmlHelper html, Expression> property)
{
return GetDisplay(property);
}
public static string GetDisplayName(this HtmlHelper> html, Expression> property)
{
return GetDisplay(property);
}
private static string GetDisplay(Expression> 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 }