在 ASP.net MVC 中通过 jQuery 在客户端本地化验证消息

发布于 2024-10-05 23:27:54 字数 519 浏览 1 评论 0 原文

我使用 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-10-12 23:27:54

也许更优雅的方法是重写 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 in ModelValidatorProviders.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文