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. This is a nice article..
    Its easy to understand ..
    And this article is using to learn something about it..

    c#,, php tutorial, Ms sql server

    Thanks a lot..!

  2. i have added

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

    Plz help me..