为什么我不能用资源的ErrorMessage与DataAnnotations? [英] Why can't I use resources as ErrorMessage with DataAnnotations?
问题描述
为什么我不能做这样的?
[必需(的ErrorMessage =*)]
[RegularEx pression(^ [A-ZA-Z0-9 _] * $的ErrorMessage = Resources.RegistrationModel.UsernameError)
公共字符串用户名{获得;组; }
什么是错误消息告诉我?
这是属性参数必须是
恒恩pression的typeof
前pression或数组创建
一个属性参数的前pression
类型。
块引用>解决方案当您使用
的ErrorMessage
属性唯一不变的字符串或字符串文字可以被分配给它。使用
ErrorMessageResourceType
和ErrorMessageResourceName
来代替specity你的资源。[RegularEx pression(
^ [A-ZA-Z0-9 _] * $
ErrorMessageResourceType = typeof运算(Resources.RegistrationModel)
ErrorMessageResourceName =UsernameError
)]请注意,该资源必须公开(可以在资源编辑器中设置)。
Why can't I do like this?
[Required(ErrorMessage = "*")] [RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = Resources.RegistrationModel.UsernameError)] public string Username { get; set; }
What is the error message telling me?
An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type.
解决方案When you are using the
ErrorMessage
property only constant strings or string literal can be assigned to it.Use the
ErrorMessageResourceType
andErrorMessageResourceName
instead to specity your resources.[RegularExpression( "^[a-zA-Z0-9_]*$", ErrorMessageResourceType=typeof(Resources.RegistrationModel), ErrorMessageResourceName= "UsernameError" )]
Note that the resources must be public (can be set in the resource editor).
这篇关于为什么我不能用资源的ErrorMessage与DataAnnotations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!