Home
Do you use MVC Unobtrusive Validation
  v4.0 Posted at 15/03/2017 3:05 PM by Brendan Richards
​Validation is an important part of any data driven web application. Client-Side validation provides fast user feedback and a better UI experience but cannot be relied on for data integrity - so client-side validation should always be backed by additional server-side validation.
With MVC Unobtrusive Validation, you can configure both client-side and server-side in one place. 

​Validation rules can be added to a model object via Data Annotations or using the Fluent Validation API.
Fluent Validation is available as a Nuget package.

DataAttributes.png

OK Example: Data Annotation attributes decorate model properties to make them required

FluentValidation.png

Better Example: Fluent Validation allows validation meta data to be added to a class without modifying the original class.  This provides much more flexibility for code reuse.


If you create a new MVC web application in VisualStudio 2013, unobtrusive validation will be enabled by default. Otherwise it's simple to install from Nuget​. To use it simply:

  1. Bind your razor views to model objects 
  2. Use Html Helpers to render the form UI​

view.png
Good Example: this razor view binds to a strongly​​ typed model object and uses html helpers.

Html.png
Figure: the HTML UI rendered for this view now has data-validation attributes that are followed by JQuery validation to provide rich client-side validation.

SaveAction.png
Figure: On the server-side, the same validation rules will be checked when you call ModelState.IsValid


Related rules

    Do you feel this rule needs an update?

    If you want to be notified when this rule is updated, please enter your email address:

    Comments:

    Note: Social Media login for Yotpo is not working in IE or Safari, please use Chrome. We are waiting for Yotpo to fix it.