ASP.NET MVC 3 Custom Validation

Version: 1.1.0

Description

In ASP.NET MVC Helpers, Forms and Validation Hands-on Lab , you have been validating data from the create and edit album forms. In this Hands-on Lab, you will use custom validation logic to control price range in the server side as well as in the client. In order to achieve that, in the first exercise you will create a custom validation property attribute and then add it to the Album model. The existing create and edit views in the StoreManager controller will process the new custom validation from the server side. In the second exercise, you will add JavaScript code and associate it to the previous custom validation, making the same views run that validation code at client side.

Overview

Note:
This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC and ASP.NET MVC validation. If you have not used ASP.NET MVC validation before, we recommend you to go over ASP.NET MVC Helpers, Forms and Validation Hand-on Lab.

In ASP.NET MVC Helpers, Forms and Validation Hands-on Lab, you have been validating data from the create and edit album forms. In this Hands-on Lab, you will use custom validation logic to control price range in the server side as well as in the client.

In order to achieve that, in the first exercise you will create a custom validation property attribute and then add it to the Album model. The existing create and edit views in the StoreManager controller will process the new custom validation from the server side.

In the second exercise, you will add JavaScript code and associate it to the previous custom validation, making the same views run that validation code at client side.

Then, you will implement a custom server-side validation using ASP.NET4 IValidatableObject Interface, performing an extra validation at server side that will run after attribute validation is performed.

Lastly, you will use ASP.NET MVC 3 Unobtrusive jQuery integration, executing the validation defined in the first exercise at client side with jQuery libraries.

Objectives

In this Hands-On Lab, you will learn how to:

  • Create a custom validation attribute to extend the validation capabilities of the solution
  • Run the validation logic at client side to improve user experience
  • Implement ASP.NET IValidatableObject to extend validation scope
  • Integrate Unobtrusive jQuery library to enhance client side validation

System Requirements

You must have the following items to complete this lab:

  • ASP.NET and ASP.NET MVC 3
  • Visual Studio 2010 Express
  • SQL Server Database (Express edition or above)

    Note:
    You can install the previous system requirements by using the Web Platform Installer 3.0: https://go.microsoft.com/fwlink/?LinkID=194638.

Setup

Installing Code Snippets

For convenience, much of the code you will be managing along this lab is available as Visual Studio code snippets. To install the code snippets run .\Source\Assets\CodeSnippets.vsi file.

Exercises

This Hands-On Lab is comprised by the following exercises:

  1. Exercise 1: Adding custom range validation
  2. Exercise 2: Adding validation at client side
  3. Exercise 3: Using IValidatableObject custom validation
  4. Exercise 4: Using Unobtrusive jQuery at client side

Estimated time to complete this lab: 45 minutes.

Note:
Each exercise is accompanied by an End folder containing the resulting solution you should obtain after completing the exercises. You can use this solution as a guide if you need additional help working through the exercises.

Next Step

Exercise 1: Adding Custom Range Validation Using Validation Attributes