Range Data Annotation Attribute in MVC

Introduction

In this blog we will see how range data annotation attribute works in MVC.

Step 1: Create asp.net web application

Choose mvc template

Setup entity framework

 

Employee.cs

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Linq;  
  5. using System.Web;  
  6.   
  7. namespace RangeAttributeDataAnnotationMVC.Models  
  8. {  
  9.     [MetadataType(typeof(EmployeeMetaData))]  
  10.     public partial  class Employee  
  11.     {  
  12.     }  
  13.   
  14.     public  class EmployeeMetaData  
  15.     {  
  16.   
  17.         [Range(18, 56, ErrorMessage="Age Must be between 18 to 56")]  
  18.         public string Age { getset; }  
  19.     }  
  20. }

Output of the application looks like this

Summary

In this blog we have seen how range data annotation works in MVC. Happy coding.

MVC Corporation
MVC Corporation is consulting and IT services based company.