2012-05-30 83 views
0
string thing = ComparaSave.Resources.Message.EmailAdrRequired; 
[Required(ErrorMessage = thing)] <---- The problem!! 
[DataType(DataType.EmailAddress)] 
[Display(Name = "Email Address")] 
[StringLength(100)] 
[EmailAddress] 
public string Email { get; set; } 

分配属性抛出错误:从RESX文件

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type.

我如何读取一个资源文件为属性,如本?

回答

4

您需要使用:

[Required(ErrorMessageResourceName= "FullNameRequired", ErrorMessageResourceType = typeof(MyResource)] 

的ErrorMessage仅用于字符串。

+0

感谢您的快速回答!作品:) – valen

+0

没问题,欢迎:) –

0

这是不可能的。属性只能使用常量值,但资源值不是常量。

您可以将ErrorMessage更改为ErrorMessageResourceName,而不是使用此处的消息文本,而是使用错误消息字符串的资源名称。在属性代码中,您需要添加将通过资源文件中的名称读取字符串的代码。

有关如何加载资源字符串的代码,请查看为.resx文件生成的.cs文件。