Ali Ali - 1 month ago 13
jQuery Question

MVC 4 client side validation not working for the form which is loaded using Ajax

I have an Admin page in which the user clicks on links and the corresponding

PartialView
, containing a web form is then loaded inside a particular div on the Admin page using Ajax.

All of the

"~/Scripts/jquery-2.0.3.js",
"~/Scripts/jquery.unobtrusive-ajax.js",
"~/Scripts/jquery.validate.js",
"~/Scripts/jquery.validate.unobtrusive.js"


are referenced within the Admin page and when the
PartialView
is loaded, the jQuery client side validation won't work.

but when I reference those scripts within the
PartialView
, everything works just fine but I don't intend to do this for each
PartialView
because they are numerous and each time each one loads, at least two of those .js files must be requested from the server again.

Is there any way I can have those scripts inside my parent (Admin) page without this issue ?

Answer

You need this on each one of your partial views:

$(document).ready(function () {

    $.validator.unobtrusive.parse("#YourFormID");

});

Basically the validation is not bound on the dynamically rendered form...