ASP.NET MVC 3 和 jQuery 验证
在 MVC 3 中进行验证的最佳方法是什么?以下是要求:
- 可以在客户端和服务器端工作。
- 在客户端和服务器之间共享尽可能多的代码(模型属性上的属性似乎是理想的)
- 跨异步请求工作
- 显示来自服务器端的错误、验证消息和成功消息
- 不显眼的 JavaScript,尽可能少
- 动态添加的 HTML 仍应验证同样,
我这个周末的任务是为此建立一个强大的解决方案,我想我会先在这里问,然后再用血汗和泪水重新发明或重新发现轮子。
What is the best way to do validation in MVC 3? Here are the requirements:
- Works client and server side.
- Shares as much code between client and server as possible (attribute on model property seems ideal)
- Works across async request
- Display errors, validation messages, and success messages coming from the server side
- Unobtrusive javascript, as minimal as possible
- Dynamically added HTML should still validate the same way
My task this weekend is to build a robust solution for this, figured I'd ask here first before re-inventing or re-discovering the wheel with blood sweat and tears.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会查看 Brad Wilson 的博客。他介绍了在 MVC3 中使用不引人注目的验证,听起来正是您正在寻找的内容。
根据OP的评论添加更多信息
关于服务器端验证(自定义验证),请查看@jfar 对我发布的有关自定义验证的类似问题的回应 - 他建议,如果您严重依赖自定义验证,则应该质疑您的设计。就我而言,我最终要么使用 Ajax 来处理我的自定义验证,要么允许回发执行验证。
I would check out Brad Wilson's blog on this. He covers using unobtrusive validation in MVC3, sounds like exactly what you're looking for.
Adding more info per OP's comment
Regarding server side validation (custom validation), check out @jfar's response to a similar question I posted regarding custom validation -- he suggests that you should question your design if you're relying heavily on custom validation. In my case, I ended up going either with Ajax to handle my custom validation, or allowed the postback to perform the validation.