Good day!
I\'ve such method to get [DisplayName] attribute value of a property (which is attached directly or using [MetadataType] attribut
Another code snippet with code .Net uses itself to perform this
public static class WebModelExtensions
{
public static string GetDisplayName(
this HtmlHelper html,
Expression> expression)
{
// Taken from LabelExtensions
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
string displayName = metadata.DisplayName;
if (displayName == null)
{
string propertyName = metadata.PropertyName;
if (propertyName == null)
{
var htmlFieldName = ExpressionHelper.GetExpressionText(expression);
displayName = ((IEnumerable) htmlFieldName.Split('.')).Last();
}
else
displayName = propertyName;
}
return displayName;
}
}
// Usage
Html.GetDisplayName(model => model.Password)