2011-07-07 31 views
2

我正在寻找一种更简单/更干燥的方法来使用资源我的MVC 3模型。在c#中为模型上的属性设置默认资源类型

这是怎么了,现在我做(需要每个属性被告知它使用的ressource型):

public class ContactMessageModel:BaseModel 
    { 
     [Display(Name="ReplyToEmail_DisplayName", ResourceType = typeof(Res.Views_Contact))] 
     public string ReplyToEmail {get; set; } 

     [Display(Name = "ContactReason_DisplayName", ResourceType = typeof(Res.Views_Contact))] 
     public string ContactReason { get; set; } 

可以这样做?

这是我想做到这一点(我只是想一次定义为模型的资源型):

[Display(ResourceType = typeof(Res.Views_Contact))] 
public class ContactMessageModel:BaseModel 
{ 
    [Display(Name="ReplyToEmail_DisplayName")] 
    public string ReplyToEmail {get; set; } 

    [Display(Name = "ContactReason_DisplayName")]    
    public string ContactReason { get; set; } 
+0

你试过了吗? –

+0

错误是:)。给我一个错误:属性'Display'在这个声明类型上是无效的。它只对'方法,属性,索引器,字段,参数'声明有效。 – AyKarsi

回答

1

似乎并不可能,因为属性实例将需要访问它所在的属性,.NET不支持。

0

是的,默认ResourceType可以完成。菲尔哈克展示如何重写.NET的ModelMetadataProviders一个例子来做到这一点,并防止其重复自己一遍又一遍地指定相同的ResourceType:

http://haacked.com/archive/2011/07/14/model-metadata-and-validation-localization-using-conventions.aspx/

您可以默认为一个单一的ResourceType全球范围内,或装饰默认使用他定义的这个属性的具体类:

public class MetadataConventionsAttribute : Attribute 
{ 
    public Type ResourceType { get; set; } 
} 
相关问题