Friday, February 4, 2011

Unobtrusive validation attributes and ViewContext.FormContext

ASP.NET MVC 3 has a new interesting feature called unobtrusive client validation. For more detailed information you may read an article by Brad Wilson. But in short it means what for an annotated model property like:


HTML helpers render such markup:


and no Javascript code is included into the page except links to external files jquery.js, jquery.validate.js and jquery.validate.unobtrusive.js.

The important thing is what all these attributes with validation rules are rendered only if ViewContext.FormContext property is initialized. It could be done with Html.BeginForm() method called in the beginning or, if you prefer using <form> tag, manually:


You should also avoid situations like the one discussed here: No unobtrusive validation if Html.BeginForm in layout template

1 comment:

  1. i have added

    ViewContext.FormContext = new FormContext(); top of the view but validation is still showing..

    Plz help me..

    Thanks

    ReplyDelete