I’ve wracked my brain on using the MetadataType / buddy class to get DisplayName / Display attributes working on my Entity Framework classes in MVC3. In my views, if I use @Html.LabelFor, I still just get the property name rather than the display attribute. My use case and set-up is very simplistic:
[MetadataType(typeof(ProductMetadata))]
public partial class Product
{
}
public class ProductMetadata
{
[Display(Name = "Why does this not work????")]
object ProductName { get; set; }
[Display(Name = "Discontinued Date")]
object DiscontinuedDate { get; set; }
}
If I then use an Html helper like LabelFor:
@Html.LabelFor(m => m.First().ProductName)
I still just get the property name in my output. Even if I do this programmatically in a custom Html extension, I just get the property name rather than the extected attribute value:
ModelMetadata.FromLambdaExpression(expression, html.ViewData).DisplayName
Any ideas or help on this would be appreciated. Is there something I am missing here?
It is not working because the properties are private in the ProductMetadata class. Change it to