Benjamin Tinker Team : Web Development Tags : Web Development

Rebinding Nonobtrusive JavaScript Validation after AJAX postbacks

Benjamin Tinker Team : Web Development Tags : Web Development

Recently I was faced with the wonderful prospect in MVC 3 of building a form that consisted of multiple page tabs controlled by jQuery and multiple postbacks. They had to be able to work exclusive of each other and with their own sets of validation rules. The nightmare was after post backs trying to rebind the jQuery validation for my explicit rules and those generated by the unobtrusive jQuery validator controls of MVC 3. Upon post back I was creating updated Views that contained new controls that required validation binding. So how could it be done. Easy, well not really. After trawling and trawling the web for an answer that was threatening my own sanity I came across a simple call.

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

And that was it. jQuery validation rebinding with $.validate(‘#form’); was not enough as this did not take into account MVC using unobtrusive validation rules. So the call above was able to cover rebinding the validation of both the explicit validation rules and all my new dynamic rules added against the new controls.

Peace out.