Data Annotations / Validation does not work for partial views

I have partial views loaded at runtime based on user input.

$("#Categories").change(function () { $.ajax({ url: "/Product/Create" + $("#Categories option:selected").text().replace(/\s+/, ""), type: "Get" }).done(function (partialViewResult) { $("#partialDiv").html(partialViewResult); }); }); 

POCOs that are used in the view model are decorated with data annotations, but they do not start.

Partial views contain a form (Html.BeginForm ()).

I think I'm doing something wrong, but I don’t know what. Any advice was greatly appreciated.

+2
asp.net-mvc-5
Aug 12 '14 at 7:40
source share
2 answers

Just include JS files ie Jquery Unobtrusive js in your Partial View, then it works fine, sometimes this problem occurs in a partial view in asp.net mvc.

Just include this js file in your Partial View as well:

 <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> 

------------------ OR try this in partial view ----------------------- ---

 $.validator.unobtrusive.parse($("form")); 
+4
Aug 12 '14 at 7:45
source share

try this if you add the form in html

 var formid = $("#frmAddProduct"); formid.unbind(); formid.data("validator", null); $.validator.unobtrusive.parse($("#frmAddProduct")); 

OR use in a partial representation of the finished document

 $.validator.unobtrusive.parse($("form")); 
0
Dec 15 '17 at 3:56 on
source share



All Articles