subject: Custom Validation In Asp.net Mvc 3 Using Data Annonations Model Validator [print this page] This article will demonstrate implementation of custom validation rules in ASP.NET MVC3 for offshore .net developers. MVC3 is an extensible framework that makes .net developers to add user defined specific custom validations with the use of custom data annotations. Considering present outsource .net development scenario the .net developers need to develop an application for Loan processing system.
In your system it will ask the customer to enter his details including Name, Salary etc. If the loan is to be processed then there is a rule that the Salary must be more than or equal to Rs 20000/-. Considering common scenario end-users need to be provided with the feedback details by which en error message would be displayed on the screen if end-user enters below the specified amount.
In case of implementing custom validation and applying it to use Data Annotations your class must inherit from ValidationAttribute class. This class is the base class for all standard data annotations types.
The first step would be to open VS2010 and create a new ASP.NET MVC 3 project. .Net developers need to name it as ASPNET_MVC3_Custom_ModelValidation.
The next step is of adding a new folder in the project and named as CustomizedValidators. Add a new class file named as CustomValidator.cs in this folder. This class contains custom validation class and data annotations class. Dot net developers need to write the below code:
ValidationAttribute class inherits the SalaryAttribute class. The SalaryAttribute class overrides the IsValid() method of the base class. An object of ValidationResult class would be returned by this method. Acontainer for the result of validation reuest is represented by this class and minSalary member is validated by the method that currently had implemented it. Here when the salary is entered below the specified amount by the end user he will receive an error message because the ValidationResult class would not return Success back to UI.
The SalaryValidator class gets inherited from DataAnnotationsModelValidator class while providing with a model validator for the specific type. For defining client validation against the specific field the method GetClientValidationRules is used.
The third step would be to addition of a new class file in the Model folder and then name it as ModelClasses.cs. Add the following model classes in it:
Locate the salary property from Customer class and the attribute Salary would be applied with the defined salary value in the constructor.
The fourth step would be adding CustomerController in the folder Controllers with code as below:
The Create () method with HttpPost attribute is used for defining the logic to create a new customer by making calls to CreateCustomer() method of the DataAccess class. With the use of ModelState.IsValid the model state is checked by the logic and it validates if the model state dictionary is valid or not.
Fifth step would be adding Index and creating views with right click on the controller methods like Index and Create. For views as customer select the Model class.
Sixth step would be to make changes like Layout.cshtml file in the shared folder: @Html.ActionLink("Customer", "Index", "Customer")
Seventh step would be to run the application and click on the Customer tab and it would be displayed as:
Click on the Create New link and the Create view will be displayed as shown below:
Enter values for CustomerName and City, keep Salary value as default and click on Submit. The result will be as shown below:
We have learnt from this article that for model validations a flexible mechanism is provided by an extensible framework like MVC 3. Benefit of creating a separate validation attributes class is that it can be reused across various models as per needs.