Dynamic partial view + jquery form hijack + client validation = not working

让人想犯罪 __ 提交于 2019-12-05 08:41:35

Upon the successfull ajax load of the new form you have to instruct the unobtrusive validation library to parse the new form by manually calling:

// insert new form into the DOM first, then call:
$.validator.unobtrusive.parse('<form selector>'));
$('<form selector>').validate(); // start validating

This sets up the jquery.validation validators according to the data-xxx attribute in the forms child elements.

After that the call $('<form selector>').validate().form() should work as expected!

Why are you trying to hijack the partial form? I think because your hijacking the form the unobtrusive live events are not firing and thats why your not getting validation errors.

Have you tried using an Ajax form? (Ajax.BeginForm(...))

If you bring in a partial ajax form this should have all the information needed for validation. Once the form is submitted you can specify a javascript function to run.

Ok finally I got it. The problem was there, no matter if I hi jacked the form or if I used Ajax.BeginForm(...), no difference there. And it always worked if I didn't loaded the partial dynamically.

The solution?

Add jquery.validate.unobtrusive.min.js to the partial view. This might be a newbie mistake, adding it only to the parent view or a master page, but if this helps someone I'll gladly admit this mistake of mine :) It's this js file that makes the client validation work and it must of course be called every time a partial (that has client validation in it) is loaded.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!