ASP.Net MVC2 客户端和服务器验证共享相同的代码 - 这可能吗?

发布于 2024-08-27 00:42:07 字数 392 浏览 5 评论 0原文

借助 Steve Sanderson 出色的 XVal,可以告诉客户端验证使用 jquery 将正在验证的值发布到服务器。然后,服务器上的方法使用与服务器端验证相同的服务器端代码,并仅返回 true 或 false 来确定该字段是否有效。

此方法的优点是,您可以在 C# 代码中编写一次复杂的验证逻辑,然后放入一些 JQuery 管道来告诉您的客户端页面在哪里访问服务器验证。

我一直在阅读一些关于 MVC2 的博客,但似乎没有人提到这一功能。是否可以告诉 Microsoft MVC 验证 javascript 调用 url 验证数据?或者您是否必须编写自己的客户端验证例程。

我应该注意,使用 xVal 方法可以通过访问服务器端验证逻辑的 JQuery post 从客户端运行自定义验证来判断电子邮件地址是否正在使用。

With the excellent XVal by Steve Sanderson, it is possible to tell the client side validation to post the value being validated to the server using jquery. A method on the server then uses the same server side code you use for your server side validation, and returns simply a true or false to determine if the field is valid.

The advantage of this method is that you write your complex validation logic once in C# code and then put some JQuery plumbing in to tell your client page where to go to access your server validation.

I have been reading some blogs on MVC2 but no one seems to mention this functionality. Is it possible to tell the Microsoft MVC validation javascript to call a url validate data? Or do you have to write your own client side validation routines.

I should note that using the xVal method a custom validation to say if an email address is in use or not can be run from the client via a JQuery post which accesses the server side validation logic.

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

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

发布评论

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

评论(2

晨光如昨 2024-09-03 00:42:07

这里有一段来自 Mix 2010 的 Phil Haack 的视频:
http://live.visitmix.com/MIX10/Sessions/FT04

有一个部分他负责编写 JS 代码来执行自定义验证......您可以使用它作为进行服务器端调用来执行验证的起点。

There's a video by Phil Haack from Mix 2010 here:
http://live.visitmix.com/MIX10/Sessions/FT04

There's a section where he deals with writing JS code to perform custom validation....you could use this as a starting point for making a server side call to perform the validation.

一个人的夜不怕黑 2024-09-03 00:42:07

我写了一篇关于此的博客文章: 使用 jquery.validate 和 xVal 进行远程表单验证

它还附带了一个演示项目。代码库仍然是 ASP.NET MVC 1,但 ASP.NET MVC 2 所需的更改很小。

I wrote a blog article exactly about this: Remote form validation with jquery.validate and xVal

It also comes with a demo project. The codebase is still ASP.NET MVC 1, but the changes required for ASP.NET MVC 2 are minor.

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