Asp.Net MVC 2 - 改变PropertyValueRequired串 [英] Asp.Net MVC 2 - Changing the PropertyValueRequired string
问题描述
使用在App_GlobalResources文件目录中的文件RESX,我已经能够更改默认的消息为模型验证的PropertyValueInvalid字符串。
Using a resx file in the App_GlobalResources directory, I've been able to change the default message for the PropertyValueInvalid string of the model validators.
但是,当需要值它不起作用翻译邮件(PropertyValueRequired。)
But it doesn't work to translate the message when a value is required (PropertyValueRequired.)
在Global.asax.cs中的Application_Start()我已经改变了资源类关键,是这样的:
In the Global.asax.cs Application_Start() I've changed the resource class key, like this:
DefaultModelBinder.ResourceClassKey = "Messages";
而在Messages.resx文件,我已经把两个条目:
And in the Messages.resx files I've put two entries:
- PropertyValueInvalid=>啊,勇敢'{0}'Einválido对Ø坎普{1}。
- PropertyValueRequired=>EnecessáriodigitarØ{0}。
感谢。
推荐答案
不使用RequiredAttribute标签DefaultModelBinder.GetValueRequiredResource。
创建自定义DataAnnotationsModelValidator类。
RequiredAttribute not used DefaultModelBinder.GetValueRequiredResource. Create custom DataAnnotationsModelValidator class.
public class MyRequiredAttributeAdapter : RequiredAttributeAdapter
{
public MyRequiredAttributeAdapter(ModelMetadata metadata,
ControllerContext context,
RequiredAttribute attribute)
: base(metadata, context, attribute)
{
attribute.ErrorMessageResourceType = typeof (Messages);
attribute.ErrorMessageResourceName = "PropertyValueRequired";
}
}
和在Global.asax中注册适配器。
and register adapter in Global.asax.
DataAnnotationsModelValidatorProvider.RegisterAdapter(
typeof(RequiredAttribute),
typeof(MyRequiredAttributeAdapter));
希望这有助于!
<一个href=\"http://ryanrivest.com/blog/archive/2010/01/15/reusable-validation-error-message-resource-strings-for-dataannotations.aspx\">Reusable验证错误消息资源字符串为DataAnnotations
这篇关于Asp.Net MVC 2 - 改变PropertyValueRequired串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!