我使用 jquery 进行客户端验证以及数据注释。一切工作正常,但我想在数字文本框中输入非数字值时本地化消息。对于服务器端验证,可以通过将 DefaultModelBinder.ResourceClassKey 设置为资源类名称并为 PropertyValueInvalid 键提供值来完成。
但是,对于客户端验证,asp.net mvc 使用其自己的资源文件,其键为“ClientDataTypeModelValidatorProvider_FieldMustBeNumeric”。我发现解决这个问题的唯一方法在使用 jQuery.validate 在 MVC 2 中自定义服务器和客户端所需的验证器。该解决方案确实有效,但我想知道是否有更简单或更正确的方法来做到这一点。
谢谢。
I am using jquery for client side validation together with data annotations. Everything is working fine but I would like to localize a message when a non numeric value is entered in numeric textbox. For server side validation it can be done by setting DefaultModelBinder.ResourceClassKey to resource class name and providing value for PropertyValueInvalid key.
However for client side validation asp.net mvc is using it's own resource file with key 'ClientDataTypeModelValidatorProvider_FieldMustBeNumeric'. The only way to solve it that I found is described at Custom Server and Client Side Required Validator in MVC 2 using jQuery.validate. The solution does work but I was wondering if there is an easier or more correct way to do it.
Thanks.
发布评论
评论(1)
也许更优雅的方法是重写 System.Web.Mvc.ClientDataTypeModelValidatorProvider 类并将其替换到 ModelValidatorProviders.Providers 集合中。
请参阅 隐式非数字字段问题部分rel="nofollow">http://jwwishart.blogspot.com/2011/03/custom-server-and-client-side-required.html 了解更多详细信息。
Maybe a bit more elegant way is to rewrite
System.Web.Mvc.ClientDataTypeModelValidatorProvider
class and replace it inModelValidatorProviders.Providers
collection.See section Implicit Non-Numeric-Field problem in http://jwwishart.blogspot.com/2011/03/custom-server-and-client-side-required.html for more details.