我绞尽脑汁使用MetadataType /伙伴类来让DisplayName / Display属性在MVC3中的实体框架类上工作。在我的视图中,如果我使用@Html.LabelFor,我仍然只获得属性名称,而不是显示属性。我的用例和设置非常简单:
[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; }
}如果我使用像LabelFor这样的Html helper:
@Html.LabelFor(m => m.First().ProductName)我仍然只是在我的输出中获取属性名称。即使我在自定义Html扩展中以编程方式完成此操作,我也只能获得属性名称,而不是extected属性值:
ModelMetadata.FromLambdaExpression(expression, html.ViewData).DisplayName任何关于这方面的想法或帮助都将不胜感激。我是不是漏掉了什么?
发布于 2012-03-15 00:21:54
它不起作用,因为属性在ProductMetadata类中是私有的。将其更改为
public class ProductMetadata
{
[Display(Name = "It works!!!")]
public object ProductName { get; set; }
[Display(Name = "Discontinued Date")]
public object DiscontinuedDate { get; set; }
}发布于 2012-03-15 00:16:03
尝试使用DisplayNameAttribute
[DisplayName("Propertyname")]
public string PropertyName {get;set;}https://stackoverflow.com/questions/9705530
复制相似问题