Please enable Javascript to correctly display the contents on Dot Net Tricks!

MVC Data Annotations for Model Validation

  Author : Shailendra Chauhan
Updated On : 31 Jan 2013
Total Views : 172,791   

Data validation is a key aspect for developing web application. In MVC, we can easily apply validation to web application by using Data Annotation attribute classes to model class. Data Annotation attribute classes are present in System.ComponentModel.DataAnnotations namespace and are availlable to projects like web application & website, MVC, Web forms and also to Entity framework orm models.

Data Annotations help us to define the rules to the model classes or properties for data validation and displaying suitable messages to end users.

Data Annotation Validator Attributes

  1. DataType

    Specify the datatype of a property

  2. DisplayName

    specify the display name for a property.

  3. DisplayFormat

    specify the display format for a property like different format for Date proerty.

  4. Required

    Specify a property as required.

  5. ReqularExpression

    validate the value of a property by specified regular expression pattern.

  6. Range

    validate the value of a property with in a specified range of values.

  7. StringLength

    specify min and max length for a string property.

  8. MaxLength

    specify max length for a string property.

  9. Bind

    specify fields to include or exclude when adding parameter or form values to model properties.

  10. ScaffoldColumn

    specify fields for hiding from editor forms.

Designing the model with Data Annotations

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
namespace Employee.Models
 [Bind(Exclude = "EmpId")]
 public class Employee
 public int EmpId { get; set; }
 [DisplayName("Employee Name")]
 [Required(ErrorMessage = "Employee Name is required")]
 public String EmpName { get; set; } 
 [Required(ErrorMessage = "Employee Address is required")] 
 public string Address { get; set; } 
 [Required(ErrorMessage = "Salary is required")] 
 [Range(3000, 10000000,ErrorMessage = "Salary must be between 3000 and 10000000")]
 public int Salary{ get; set; } 
 [Required(ErrorMessage = "Please enter your email address")] 
 [Display(Name = "Email address")]
 [RegularExpression(@"[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}", ErrorMessage = "Please enter correct email")]
 public string Email { get; set; }

Once we have define validation to the model by using data annotations, these are automatically used by Html Helpers in views. For client side validation to work, please ensure that below two <SCRIPT> tag references are in the view.

 <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> 
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> 

Presenting the model in the view

 @model Employee.Models
 ViewBag.Title = "Employee Details"; 
 Layout = "~/Views/Shared/_Layout.cshtml";
 @using (Html.BeginForm())
 <div class="editor-label">
 @Html.LabelFor(m => m.EmpName)
 <div class="editor-field"> 
 @Html.TextBoxFor(m => m.EmpName) 
 @Html.ValidationMessageFor(m => m.EmpName)
 <div class="editor-label">
 @Html.LabelFor(m => m.Address)
 <div class="editor-field">
 @Html.TextBoxFor(m => m.Address) 
 @Html.ValidationMessageFor(m => m.Address)
 <div class="editor-label"> 
 @Html.LabelFor(m => m.Salary)
 <div class="editor-field"> 
 @Html.TextBoxFor(m => m.Salary) 
 @Html.ValidationMessageFor(m => m.Salary)
 <div class="editor-label">
 @Html.LabelFor(m => m.Email)
 <div class="editor-field"> 
 @Html.TextBoxFor(m => m.Email) 
 @Html.ValidationMessageFor(m => m.Email)
 <p> <input type="submit" value="Save" />

In this article I try to expose the Data Annotations with example. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

Free Interview Books
13 FEB
ASP.NET Core Development (online)

07:00 AM-09:00 AM IST( MON, WED, FRI)

Know More
25 JAN
Angular2 and Angular1 Development (online)

09:00 PM-10:30 PM IST(MON-FRI)

Know More
24 JAN
ASP.NET MVC with Angular2 Development (online)

07:00 AM-09:00 AM IST(Tuesday & Thursday)

Know More
21 JAN
Xamarin Forms : Build Cross-platform Apps (offline)

05:00 PM-06:30 PM IST

Know More
20 JAN
NODEJS & MEAN Stack 2 Development (online)

09:00 PM-11:00 PM IST( MON, WED, FRI)

15 JAN
PPC Marketing (offline)

04:00 PM-05:300 PM

10 JAN
ASP.NET MVC with AngularJS Development (online)

09:00 PM-11:00 PM IST on (TUE, THRU,SAT)

ASP.NET MVC with AngularJS Development (online)

07:00 AM-09:00 AM IST

31 DEC
ASP.NET MVC with AngularJS Development (offline)

09:30 AM-11:00 AM

31 DEC
Angular2 and Angular1 Development (offline)

11:00 AM-12:30 AM

26 NOV
ASP.NET MVC with AngularJS Development (offline)

03:00 PM-04:30 PM

ASP.NET MVC with AngularJS Development (offline)

08:00 AM-09:30 AM

MEAN Stack 2 Development (offline)

11:00 AM-12:30 PM.


Professional Speaks