Client side validation after Ajax Partial View result in ASP.NET MVC 3

Now, as ASP.NET MVC 3 beta was finally released I have decided to give it a try.

The most exciting thing that I have noticed is implementation of an idea of so called Unobtrusive JavaScript. This is for both Ajax helpers and Client Validation. And I have to admit that I just love it.

I wll just copy and paste text from scottgu blog:

Unobtrusive JavaScript and HTML 5: The AJAX and Validation helpers in ASP.NET MVC now both use an unobtrusive JavaScript approach by default. Unobtrusive JavaScript avoid injecting inline JavaScript into HTML, and instead enables cleaner separation of behavior using the new HTML 5 data- convention (which conveniently works on older browsers as well). This makes your HTML smaller and cleaner, and makes it easier to optionally swap out or customize JS libraries.  The Validation helpers in ASP.NET MVC 3 also now use the jQueryValidate plugin by default.

Basically we get all the validation done by loading js function parsing loaded DOM searching for specific parameters based on which validation rules are applied to elements. And all of that without a single js line from ‘our’ – web developers side.

One thing is not obvious though and it certainly needs extra explanation: what happens after part of the page is loaded via Ajax request (common Partial View result). Validation is not turned for those elements although all attributes are correctly marked. I have dig a little and found within JavaScript file ‘jquery.validate.unobtrusive.js’ function: jQuery.validator.unobtrusive.parse(selector). Calling function after new content is loaded solves problem, like in the example below:


$.post("test/updateCustomer", { name: "Mark", surname: "Barker" },
  function(htmlContent){
    $('#container').html(htmlContent);
    jQuery.validator.unobtrusive.parse('#content')
  }
);

Hope it helps someone…

Share

Related posts:

  1. Checkbox has to be ‘checked’ – with unobtrusive jQuery validation and ASP.NET MVC 3
  2. Tips for jQuery validation plugin
  3. Javascript Encode on server side – Medium Trust Environment
  4. ASP.NET MVC 1.0 issue within HtmlHelper.GetModelStateValue method
  • Pingback: Tweets that mention Client side validation after Ajax Partial View result in ASP.NET MVC 3 | ITmeze -- Topsy.com

  • Andrey Kuleshov

    Thanks for your post!
    Yes, it was helpful :)

  • mrnon

    You save my life! Thanks a lot :D

  • http://www.getcheapflowers.co.uk Cheap Flower Guy

    Thanks very much. Having seen Brad Wilson’s post, I misread it and had been trying to call parse without a parameter which was not working!

  • phil

    fabulous find – started to panic thinking it wasnt possible!

  • http://ITmeze.com ITmeze

    i am glad You found it helpful.
    The reason I posted that was because I was having problems solving that as well.

  • Toi

    Thank you very much ITmaze, you save my life. :)

  • http://naspinski.net naspinski

    Fantastic! You just saved me who knows how many hours!

  • http://thedutchpirates.com Skywalker

    Excellent, this saved me a lot of headache! :) Thanks!

  • Jeff

    Excellent! Thanks very much :)

  • Guest

    I’ve added this code and I still have a problem. The textbox’s contained within the loaded PartialView have the “Required” DataAnnotation on them and the associated message displays right away. How can I avoid this? Thanks in advance!

  • http://thinkcode.pl Szczepan

    Thanks. Exactly what I was looking for.

  • Nico

    But you can also add this on the top of the partial view

    // wait for the DOM to be loaded
    $(document).ready(function () {
    jQuery.validator.unobtrusive.parse($(“#content”));
    });

  • Chamadi Baba

     Same problem i m faceing..’(

  • Szymon Drosdzol

    Great! I took me hours and hours looking for solution until I came to this place. Many thanks :)