Introduction
This article explains the Data Annotation property for doing validation. Here we create a simple form for displaying the validation using Data Annotation. We know that validation is very important for any form.
Use the following procedure to create a simple form.
Step 1
Create a Web API Application as in the following:
- Start Visual Studio 2013.
- From the Start Window select "New Project".
- Select "Installed" -> "Templates" -> "Visual C#" -> "Web" -> "Visual Studio 2012" and select "ASP.NET MVC4 Web Application".

- Click on the "OK" button.
- From the MVC4 project window select "Web API".

- Click on the "Create Project" button.
Step 2
Add a Model Class as in the following:
- In the "Solution Explorer".
- Right-click on the Model Folder.
- Select "Add" -> "Class".
- Select "Installed" -> "Visual C#" and select "Class".

- Click on the "Add" button.
Add the following code:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Linq;
- using System.Web;
- namespace ValidationData.Models
- {
- public class UserModel
- {
- [Required(ErrorMessage = "Enter User Name")]
- public string FirstName { get; set; }
- [Required(ErrorMessage = "Enter User Last Name")]
- public string LastName { get; set; }
- [Required(ErrorMessage = "Enter USer Address")]
- public string Address { get; set; }
- [Required(ErrorMessage = "Enter User Age")]
- [Range(18, 50, ErrorMessage = "Age Sholud more the 18 and Less Than 50")]
- public int Age { get; set; }
- }
- }
Step 3
Add a Controller:
- In the "Solution Explorer".
- Right-click on the Controller folder, select "Add" -> "Controller".
- From the Template select "MVC Controller".

- Click on the "Add" button.
Add the following Code:





Comments
Join the conversation! Your thoughts help the community grow.