2

I have some 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);
        });
    });

The POCO's that are used in the view model are decorated with data annotations, but they are not triggered.

The partial views each contain a form (Html.BeginForm()).

I guess I'm doing something wrong, but not sure what. Any advice greatly appreciated.

user3907267
  • 238
  • 4
  • 8
  • What exactly do you mean when you say that the 'data annotations are not triggered'? Do you mean form validation does not work? That's a different problem. – Knelis Aug 12 '14 at 07:43
  • The problem is that your dynamically adding the form to the DOM. You need to re parse the validator. [This article](http://memoriesdotnet.blogspot.com.au/2011/11/unobtrusive-validation-in-jquery-has.html) should help. –  Aug 12 '14 at 07:43
  • @CornéM Yes, that is what I mean. – user3907267 Aug 12 '14 at 07:47
  • Thanks @StephenMuecke. Will take a look at that now. – user3907267 Aug 12 '14 at 07:48

2 Answers2

5

Just Include JS files i.e Jquery Unobtrusive js file in your Partial View also then it work fine ,some times this problem comes in partial view in asp.net mvc.

Just include this js file in your Partial View also :

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

------------------OR try this in Partial View--------------------------

$.validator.unobtrusive.parse($("form"));
Kartikeya Khosla
  • 18,743
  • 8
  • 43
  • 69
1

try this if you are appending form in html

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

OR use in partial view on document ready

$.validator.unobtrusive.parse($("form"));
Karan Singh
  • 876
  • 1
  • 6
  • 12