Good day!
I\'ve such method to get [DisplayName]
attribute value of a property (which is attached directly or using [MetadataType]
attribut
Late to the game, but...
I created a helper method using ModelMetadata like @Daniel mentioned and I thought I'd share it:
public static string GetDisplayName(
this TModel model
, Expression> expression)
{
return ModelMetadata.FromLambdaExpression(
expression,
new ViewDataDictionary(model)
).DisplayName;
}
Models
:
public class MySubObject
{
[DisplayName("Sub-Awesome!")]
public string Sub { get; set; }
}
public class MyObject
{
[DisplayName("Awesome!")]
public MySubObject Prop { get; set; }
}
Use
:
HelperNamespace.GetDisplayName(Model, m => m.Prop) // "Awesome!"
HelperNamespace.GetDisplayName(Model, m => m.Prop.Sub) // "Sub-Awesome!"