ASP.NET MVC Model Validations Using Data Annotations

Introduction

In this article, I will explain how to implement model validations using data annotations in ASP.NET MVC. In ASP.NET MVC, we can easily implement validation by using Data Annotation properties. We can see the practical example for creating ASP.NET MVC model validation using Data Annotation.

  • Open Visual Studio installed in your system.

  • Go to File -> Select New - Project.

    ASP.NET

  • Select Template >> ASP.NET Web application and give the appropriate application name followed by clicking on OK.

    ASP.NET

  • When you click OK button, a new popup will open.

  • Select Empty Template and also select checkbox of "Add folders and core references as MVC". Then, click OK.

    ASP.NET

  • Once your application creation is completed, your project structure would be like this.

    ASP.NET

  • The next step is to right click on your "Models" folder >> Add >> select Class and name it as UsersModel.

    ASP.NET
    1. using System.ComponentModel.DataAnnotations;  
    2. namespace MVCExamples.Models {  
    3.         public class UsersModel {  
    4.             [Key]  
    5.             public int UserId {  
    6.                 get;  
    7.                 set;  
    8.             }  
    9.             [RegularExpression("^[a-zA-Z]*$", ErrorMessage = "Only Alphabets are Allowed")]  
    10.             [Required(ErrorMessage = "Please Enter Username")]  
    11.             public string UserName {  
    12.                 get;  
    13.                 set;  
    14.             }  
    15.             [Required(ErrorMessage = "Please Enter Password")]  
    16.             public string Password {  
    17.                 get;  
    18.                 set;  
    19.             }  
    20.             [Required(ErrorMessage = "Please Enter Confirm Password")]  
    21.             [Compare("Password", ErrorMessage = "Both Password and Confirm Password Must be Same")]  
    22.             public string ConfirmPassword {  
    23.                 get;  
    24.                 set;  
    25.             }  
    26.             [Required(ErrorMessage = "Please Enter Location")]  
    27.             public string Location {  
    28.                 get;  
    29.                 set;  
    30.             }  
    31.         }  

  • In the above code, we just added Data Annotation reference and used several validation properties like Required, Compare, Regular Expression. Required is generally used for required fields and Compare is used to compare the properties etc. based on our requirements.

  • Now, add a new Controller. For that, right click on your Controller folder >>  select Add >> select Controller as shown below.

    ASP.NET

  • Next step is to click on Controller. A new popup will open. Then, select MVC 5 Controller – Empty. Click "Add", as shown below.

    ASP.NET

  • When you click on "Add" button, a new window will open. Give the name to your Controller and click on "Add" button. Your new Controller file will be added to the folder.

  • Open new Controller (UserController) and write the code given below.
    1. using System.Web.Mvc;  
    2.   
    3. namespace DataAnnotationExample.Controllers  
    4. {  
    5.     public class UserController : Controller  
    6.     {  
    7.         // GET: User  
    8.         public ActionResult Index()  
    9.         {  
    10.             return View();  
    11.         }  
    12.   
    13.         public ActionResult UserRegistration()  
    14.         {  
    15.             return View();  
    16.         }  
    17.     }  
    18. }  
  • Now, right click on UserRegistration method and select "Add View", as shown below.

    ASP.NET

  • When you click on "Add View", a new template will open. Select Template type “Create” and also you need to select Model class as “User Model”. Then, click on Add.

    ASP.NET

    ASP.NET

  • Now, "Run" your application.
  • Click on "Create" and you will see the validation that you have implemented.

    ASP.NET

The other validation looks like below.

ASP.NET
Summary

In this article, we learned how to perform ASP.NET MVC Model Validations using Data Annotations. I hope you like it.