MVC Validation – Part 1 – Model Data Annotations

15 Jun

Most Web Applications will require some form of user input and it is often a requirement to validate this input.  With ASP.NET web forms validation could be performed using the validator controls and were often implemented as an afterthought e.g. RequiredFieldValidator, RegularExpressionValidator etc.

Within the world of MVC validation takes another approach.  Using Data Annotations we can apply validation directly to the model objects easily, allowing the validation rules to filter through to the objects that consumes these model objects.

In Part 1 of my posts on MVC Validation I will provide a step by step guide to applying validation to a model object within my solution.

Step 1 – Create an MVC Project

Before we can apply the validation lets create a simple MVC project and when prompted set the view engine to Razor.

The project template will look similar to this:

MVC Solution Template

In order to ensure we can use Data Annotations for enforce our validation make sure there is a reference to System.ComponentModel.DataAnnotations
Step 2 – Create our model class and apply the validation

Within the Models Folder add a new class named Customer and create a few properties e.g.

Customer Class

Also add the using statement for the DataAnnotations reference added earlier

So now we have our model object we can use data annotations to apply validation to properties of the object.

For example, assume that the Forename and Surname and required fields and that the length cannot exceed 50 characters.  Also assume that the email address is also required and that the users input for the email address must be validated using regular expressions.  To enforce these rules we could modify the class using the following annotations:

Validation Decorators

Note: Other annotations available are:

Range: Validates whether a value falls between a minimum and maximum value

DataType: Validates whether the value matches a certain type e.g. could be used to ensure the user supplies a valid date time value

Step 3 – Create a controller and view then run the application

The final stage is just to create a controller class to test the validation.

Right-click on the Controllers folder then click Add à Controller…

Add the following two Create methods

Controller Class

Notice the Create() with the [HttpPost] annotation.  This is the method that is called when the user posts data from the Customer View back to the Server and so this is the method which will invoke the validation check.

Simply call ModelState.IsValid to determine whether or not the user’s input conforms to the validation rules
imposed on the model.

To create the View (our user interface), right-click the Create() Method in the controller and click Add View…

Add View Screen

Click ‘Create a strongly-typed view’ and select our Customer model object.  Also ensure that the Scaffold template is set to ‘Create’.

All that is left now is to run the application.  You may need to modify the invoked URL to be
something like http://localhost:50712/Customer/Create  so that our Create new Customer View isinvoked

Try leaving the Forename and Surname fields blank and putting rubbish into the email field then hitting the Create button, Voila!! Validation without any Validator Controls in sight


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: