问题描述
有很多问题(和答案)的这里计算器和其他网站关于翻译的DefaultModelBinder提供的默认的错误信息,如的或this 之一。
There's a lot of question (and answer) here on stackoverflow and other site about translating the default error messages provided by the DefaultModelBinder, such as this one or this one.
所有这些问题的答案基本上proprose创建的ressource(RESX)下App_GlobalResources文件文件,并把一个消息PropertyValueInvalid。
All answers to these questions basically proprose to create a ressource (resx) file under App_GlobalResources and put a message for "PropertyValueInvalid".
这很好,它的确实的工作,但有的其他的消息也(前的值XXX必须是一个数字)和最大的问题是:在哪里在地狱里,我可以有消息键的列表用于模型绑定验证,与它们用于这样我就可以翻译他们每个人的情况下的描述?
That's fine, it does works, but there is other message also (ex. "The value xxx must be a number") and the big question is : where in hell can I have a list of the messages key used for model binding validation, with a description of the context they are used for so I can translate each of them ?
推荐答案
在 DefaultModelBinder.ResourceClassKey =ResourceFileName;
在Application_Start方法中设置DefaultModelBinder类使用其他资源按键比由Asp.Net MVC使用的资源文件中定义的。
When DefaultModelBinder.ResourceClassKey = "ResourceFileName";
is set in Application_Start method the DefaultModelBinder class uses other resource keys than defined in the resource file used by Asp.Net MVC.
虽然拆卸DefaultModelBinder有从自定义资源文件中读取的方法GetUserResourceString。此功能只与这些消息键叫了两声:
While disassembling DefaultModelBinder there is a method GetUserResourceString that reads from the custom resource file. This functions is only called twice with these messages keys:
- PropertyValueRequired (A值是必需的。)
- PropertyValueInvalid (值{0}是无效的{1})
- PropertyValueRequired (A value is required.)
- PropertyValueInvalid (The value '{0}' is not valid for {1}.)
这些是可以为DefaultModelBinder在MVC3设置的唯一消息。
These are the only messages that could be set for DefaultModelBinder in MVC3.
这篇关于asp.net MVC3 DefaultModelBinder错误信息键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!