Data Entry and Validation in ASP.NET MVC Framework Applications

WEBINAR:On-Demand

Introduction

The ASP.NET MVC Framework (based on the Model View Controller Design Pattern) is a platform for designing and developing web applications on top of the managed ASP.NET runtime. Since ASP.NET MVC applications doesn't rely on the traditional ASP.NET model, validating user input is a bit tricky. Scott Guthrie states in his blog: "Validating user-input and enforcing business rules/logic is a core requirement of most web applications. ASP.NET MVC 2 includes a bunch of new features that make validating user input and enforcing validation logic on models/viewmodels significantly easier. These features are designed so that the validation logic is always enforced on the server, and can optionally also be enforced on the client via JavaScript."
Reference: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

This article discusses the basics of ASP.NET MVC Framework and how it can be used to perform CRUD operations and implement validation logic in the presentation layer.

Pre-requisites

To execute the code examples illustrated in this article, you should have the following installed in your system:

ASP.NET MVC 2.0

Microsoft Visual Studio 2008

Alternatively, you can have Microsoft Visual Studio 2010 installed in your system - you'll get the ASP.NET MVC Framework and the necessary templates you need to work embedded there.

The Model View Controller Design Pattern - Why do you need it?

Application that have a mix of data access, business logic and presentation layer code are difficult to test and maintain because of the interdependencies amongst the components. A change in any of such components would incur a change in the components that depend on the component that has changed. The MVC Design Pattern solves these problems by reducing the cohesion amongst the components in an application. It does this by decoupling the data access, business logic, and data presentation and user interaction components resulting in loosely coupled and easily testable components.

The Model View Controller is one of the most popular and widely used of all design patterns. It is used to facilitate testability, promote a cleaner separation of concerns and also for easier maintenance of the application's code. It should be noted that an application's concern can be of two types--the core concerns (these include the application's business logic and data access logic, etc) and cross cutting concerns (these include logging, exception handling, messaging, etc).

The Model View Controller Design Pattern is comprised of the following three core components:

Model - it is the component that is responsible for handling the application's business logic and data access components.

The View - it is the component that is responsible for presenting the model's data in the user interface.

The Controller - it is the component that manages the interaction amongst the other components and invalidates the View based on the Model's state.

What is the ASP.NET MVC Framework?

The ASP.NET MVC Framework is based on the proven time tested MVC Design Pattern and provides you a platform for designing and implementing web applications where you can have a cleaner separation of concerns, better code organization, seamless extensibility, scalability and code reuse. Applications designed using the ASP.NET MVC Framework is easier to test and maintain. Scott Guthrie states in his blog: "One of the benefits of using an MVC methodology is that it helps enforce a clean separation of concerns between the models, views and controllers within an application. Maintaining a clean separation of concerns makes the testing of applications much easier, since the contract between different application components are more clearly defined and articulated."
Reference: http://weblogs.asp.net/scottgu/archive/2007/10/14/aspnet-mvc-framework.aspx

The ASP.NET MVC Framework provides support for the following features:

Support for a Test Driven Design model

Support for Dependency Injection and IOC containers

Support for clean URLs and navigation support

Support for REST-based design

Pluggable, extensible, and maintainable

Support for all existing ASP.NET features that includes, authentication, authorization, membership and role management, state management, etc.

Implementing Validation Logic in ASP.NET MVC Applications

Validating user input in ASP.NET web forms is easy--you have so many controls that help you get the job done. Also, retrieving the data posted from a web form is simple. In ASP.NET MVC applications, you can validate user input using built-in HMTL helper functions and ModelState class.

Model Binding is a powerful feature in ASP.NET MVC Framework. It validates the posted data against the validation rules that have been specified on the model class. If the data being posted doesn't conform to the validation rules, the Model Binding framework appends the errors to the ModelState object. Incidentally, the ModelState object is an instance (it's a Dictionary to be precise) that contains all the errors that have occured due to violation of the validation rules while the data was posted by the web form. You can use the IsValid property of the ModelState class to determine what to do if a validation failure has occured. Here's a code snippet that illustrates this concept:

Now, let's update the model with validation tags and error messages to enforce validation rules. To do this you should add the System.ComponentModel.DataAnnotations namespace to your model class and then decorate its properties using the [Required], [StringLength], [Range], and [RegularExpression] validation attributes provided by this namespace we just added.

One of the amazing things in the validation framework in ASP.NET MVC 2.0 is in its seamless support for both server-side and client-side validation. To enable client-side validation in your ASP.NET MVC application, all you have to do is add the MicrosoftAjax.js and MicrosoftMvcValidation script files as JavaScript references and then make a call to Html.EnableClientValidation() in your code. Refer to the code snippet below:

Summary

The ASP.NET MVC Framework provides .NET developers a platform to design and implement web applications in a more structured way, make them testable, maintainable and extendable and, with less code. While designing web applications, you would often need to validate user input and then enforce business rules/logic based on the outcome. The new versions of ASP.NET MVC (version 2.0 and later) comes in with enhanced support for implementing validation logic on your model classes easily .In this article we have had a look at how the Model Binding framework in ASP.NET MVC can be used to implement validation logic.