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. namespace RangeAttributeDataAnnotationMVC.Models
  7. {
  8. [MetadataType(typeof(EmployeeMetaData))]
  9. public partial class Employee
  10. {
  11. }
  12. public class EmployeeMetaData
  13. {
  14. [Range(18, 56, ErrorMessage="Age Must be between 18 to 56")]
  15. public string Age { get; set; }
  16. }
  17. }

Output of the application looks like this

Summary

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