当我在ASP.NET MVC 3模型中使用DisplayAttribute时,它很快就变成了写它们的痛苦,因为我们必须对字符串进行硬编码或者从包含const strings
(这是我所拥有的某个静态类引用字符串现在,见下文)。但即使这对我来说太过分了。自定义数据注解属性
我想拿出那被称为像[SimpleDisplay],它会含蓄地看着
- 类名构建资源字符串的属性,
- 属性名的属性被附加到。
这可能吗?
像这样的事情
public class Product {
[SimpleDisplay] // it will take Product and Name and do something like this Product_Name
public string Name { get; set; }
}
这是我想摆脱的,如果可能的话:
[Display(ResourceType = typeof(Resources.Localize), Name = ResourceStrings.product_prettyid)]
public virtual int PrettyId
{
get;
set;
}
[Display(ResourceType = typeof(Resources.Localize), Name = ResourceStrings.product_name)]
public virtual string Title
{
get;
set;
}
现在我知道这是不可能的继承DisplayAttribute原因它的密封。我还有什么其他选择?它有意义吗?
谢谢,这是最好的方法。有关您的方法的更多详细信息,请参阅http://geekswithblogs.net/brians/archive/2010/06/14/asp.net-mvc-localization-displaynameattribute-alternatives-a-better-way.aspx – mare 2011-02-10 20:21:36