Search code examples
asp.net-mvctwitter-bootstraprazorasp.net-mvc-5partial-views

What is the reson behind using $("form").removeData("validator") & $("form").removeData("unobtrusiveValidation"); inside partial view


I am reading the following example link about displaying a partial view inside a popup menu. but i have noticed that inside the partial view the author uses the following code at the end of the view:-

$("form").removeData("validator");
    $("form").removeData("unobtrusiveValidation");
    $.validator.unobtrusive.parse("form");

so can anyone advice what is the purpose behind adding this code?


Solution

  • It removes the jQuery validation from the form. Here is a reference to the validation data.

    var form = $(formSelector)
      .removeData("validator") /* added by the raw jquery.validate plugin */
      .removeData("unobtrusiveValidation");
        /* added by the jquery unobtrusive plugin */
    

    To be specific to the implementation in partial view, you can implement the validation with a method like this

    function ApplyValidation() {
        $("form").removeData("validator");
        $("form").removeData("unobtrusiveValidation");
        $.validator.unobtrusive.parse("form");
    }