Get in touch

Get in touch with Binary

Binary Studio website uses cookies to enhance your browsing experience. By continuing to use our site, you agree to our Privacy Policy and use of cookies.

Learn more more arrow
Binary Studio 12.08.2009

Creating validation in ASP.NET MVC 1.0

This is an article from the ASP.NET MVC software development series. This article describes one method of performing validation in an ASP.NET MVC application. In this tutorial, you learn how to move your validation logic out of your controllers and into a separate layer.

The goal of this post is to describe one method of performing validation in an ASP.NET MVC application (asp.net mvc outsourcing). In this post, I’ll show how to move validation logic out of controllers and into a separate service layer. Integrating validation and business logic is a key part of any application that works with data. For the beginning I would like to describe steps of building the validation.

Creating Data Model Classes with LINQ to SQL

LINQ to SQL enables us to quickly create data model classes from existing database schema. To-do this we’ll open the database in the Server Explorer, and select the Tables we want to model in it.

Imagine we have a LINQ to SQL data model, containing UserSettings and aspnet_User from the Simple_db database. As you may know, LINQ to SQL will generate these classes as partial classes, which we can use to extend this domain object’s behaviour

Selecting tables, which need modeling in database.

We can then drag the tables onto the LINQ to SQL designer surface. When we do this LINQ to SQL will automatically create classes using the schema of the tables (with class properties that map to the database table columns).

Visual Studio automatically generates .NET classes that represent the models and database relationships defined using the LINQ to SQL designer. A LINQ to SQL Data Context class is also generated for each LINQ to SQL designer file added to the solution.

Integrating Validation and Business Logic with Model Classes

Creating a DataAdapter for manipulations with Data Models

Adding a UserSettings Actions in Account Controller

Adding a UserSettings View

During the development of the controller action method, creating a corresponding view is very straightforward. To create a new view for the current controller action, right-click somewhere on the method body, and select Add view... from the context menu. The following dialog box will be displayed.

Dialog box.

In the Add view dialog box, some options can be specified. First of all, the view name can be modified if required. By default, this name will be the same as the action method name. It's also possible to select a view template, which we will set to Empty. This template can be used to easily create a view for example, one which shows the details of an employee.

Within our UserSettings() action method we attempt to retrieve a UserSettings object using the id provided within the URL. If a valid UserSettings is found we call the View() helper method, indicating we want to use a “UserSettings” view template to render the retrieved UserSettings object.

Now, because we already have Html.ValidationMessage() helpers in the view, our error messages will appear:

Error messages box in MVC application.


The aim of this post was to discuss one approach to performing validation in an ASP.NET MVC application. In this post, I showed how to move all of your validation logic out of your controllers and into a separate service layer. Hope it was of some help to you.



The Official Microsoft ASP.NET Site

ScottGU’s Blog



Denis K.,
.NET team, Binary Studio