您可以通过 AJAX / jQuery 调用使用数据注释/验证吗?如果是这样,请提供一个示例或显示示例的帖子。
基本上我已经看到了如何使用数据注释的示例,但是它带有完整的回发。有没有办法处理 AJAX / jQuery 调用?不确定您将如何执行此操作,因为我不确定您将如何在客户端构建模型对象。 (我认为这是你必须做的。)
有人告诉我这是可以做到的,但我只是不明白这是怎么回事。
感谢您的帮助。
Can you use Data Annotations / Validation with an AJAX / jQuery call? If so, please provide an example or a post which shows an example.
Basically I have seen an example of how to use Data Annotations, however it was with a full post back. Is there a way to do with an AJAX / jQuery call? Not sure how you would do this since I am not sure how you would construct the Model object on the client side. (I assume this is what you would have to do.)
Someone told me this can be done, but I just don't understand how it can be.
Thanks for your help.
发布评论
评论(2)
是的 - 100% 我一直这样做。使用 Ajax.BeginForm 并使用不显眼的验证
http://completedevelopment.blogspot.com/2011 /02/unobstrusive-javascript-in-mvc-3-helps.html
这将发出客户端所需的所有内容..尽管您必须再次连接验证器来告诉 jQuery我们已经加载了一些需要验证的内容
我相信我从以下位置得到了这个想法/代码: http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
不管怎样,我下面的内容都有效,而且我目前正在使用它,尽管我添加了一些调试。
然后在使用 ajax 加载的每个部分视图(或页面)中,包含以下内容:
(注意 editCustomerAddress 是包含我的新内容的 div 名称,因此 jQuery 不必重新解析我页面上的所有内容,而只需从我的动态内容开始)
此外 - 您希望确保您的部分视图不没有自己的 ajax 或 html 表单,您需要通过
@{ 拥有表单上下文
if(ViewContext.FormContext == null) {ViewContext.FormContext = new FormContext();
否则
,辅助方法将不会发出您的 data-val-* 属性。如果您的视图没有 Ajax.BeginForm 或 Html.BeginForm 来创建自己的表单上下文,则需要这个。
Yes - 100% I do it all the time. Use Ajax.BeginForm and use unobtrusive validation
http://completedevelopment.blogspot.com/2011/02/unobstrusive-javascript-in-mvc-3-helps.html
this will emit everything you need client side.. although you have to hook up the validators again to tell jQuery that we have loaded some content that it will need to validate
I believe I got this idea/code from: http://xhalent.wordpress.com/2011/01/24/applying-unobtrusive-validation-to-dynamic-content/
either way what I have below works and I'm currently using it although I added a little debugging to it.
then in each partial view (or page) that you use ajax to load, have this:
(note editCustomerAddress is the div name that contains my new content, so jQuery doesnt have to reparse everything on my page but only from my dynamic content down)
Also - you want to make sure your partial views that don't have their own ajax or html forms you need to have a form context via
@{
if(ViewContext.FormContext == null) {ViewContext.FormContext = new FormContext();
}
otherwise your data-val-* attributes will not be emitted by the helper methods. You need this if your views do not have an Ajax.BeginForm or Html.BeginForm to create their own form context.
如果您使用 Html.AjaxForm(而不是 Html.BeginForm),那么它将使用 Ajax 进行验证。
但是我不认为你可以使用 jQuery 进行验证。 Microsoft 有自己的 Ajax 库,他们自己调用/维护它。我不认为你可以在两者之间挂钩你自己的代码。
If you use Html.AjaxForm (instead of Html.BeginForm) then it will validate using Ajax.
However I don't think you can validate using jQuery. Microsoft has it's own Ajax libraries and they call/maintain it themselves. I don't think you can hook your own code in between.