---
title: "Model Validation in Asp.net MVC 4"  
description: "In this blog, I’m explaining how to do model validation in asp.net mvc 4.Step 1:Open Visual Studio 2012 and create an asp.net mvc 4 application and ad"  
author: "Sumit Kesarwani"  
published: 2014-02-10  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/646/model-validation-in-asp-dot-net-mvc-4  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 3 minutes  

---

# Model Validation in Asp.net MVC 4

In this blog, I’m explaining how to do [model validation](https://www.mindstick.com/forum/160269/what-is-required-data-annotation-how-does-it-affect-model-validation) in [asp.net mvc](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) 4.

Step 1:

Open [Visual Studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) 2012 and create an asp.net mvc 4 application and add a controller named “HomeController.cs” to the controller folder in the solution explorer.

```
using ModelValidationApp.Models;using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace ModelValidationApp.Controllers{    public class HomeController : Controller    {        //        // GET: /Home/        [HttpGet]        public ViewResult Index()        {            return View();        }    }}
```

##### Step 2:

Next add a model class named “Student.cs” like below:

```
using System;using System.Collections.Generic;using System.ComponentModel.DataAnnotations;using System.Linq;using System.Web;namespace ModelValidationApp.Models{    public class Student    {        [Required(ErrorMessage="Name is required")]//Make the field required        public string name { get; set; }        [Required(ErrorMessage="Address is required")]        public string address { get; set; }        [Required(ErrorMessage="Email is required")]        [RegularExpression(".+\\@.+\\..+", ErrorMessage = "Invalid Email")]//Check for valid email address        public string email { get; set; }    }}
```

In an [MVC application](https://www.mindstick.com/forum/452/do-i-need-to-install-mvc-3-4-on-web-server-to-run-mvc-application), validation is typically applied in the domain model, rather than in the [user interface](https://www.mindstick.com/interview/22909/user-interface-objects-in-cocoa). This means that we define our validation criteria in one place, and it takes effect in any place the model class is used. ASP.NET MVC supports declarative [validation rules](https://www.mindstick.com/forum/158293/is-it-possible-to-create-custom-validation-rules-using-jquery-validation-if-so-how) defined with attributes from the **System.ComponentModel.DataAnnotations** namespace.

##### Step 3:

Now add a view to the project by right clicking on the “Index()” method in HomeController.cs and named it “Index.cshtml” and make it a strongly-typed view like below:

```
@model ModelValidationApp.Models.Student@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Index</title></head><body>    <div>        <h2>Registration Form</h2><br />        @using(Html.BeginForm())        {            <p>Name : @Html.TextBoxFor(m=>m.name) @Html.ValidationMessageFor(m=>m.name)</p>            <p>Email : @Html.TextBoxFor(m=>m.email) @Html.ValidationMessageFor(m=>m.email)</p>            <p>Address : @Html.TextBoxFor(m=>m.address) @Html.ValidationMessageFor(m=>m.address)</p>            <input type="submit" value="Submit"/>                }    </div></body></html>
```

@Html.ValidationMessageFor() – will display the [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) according to the field.

##### Step 4:

Now add another viewresult to the HomeController.cs like this:

```
public ViewResult Index(Student student)        {            if (ModelState.IsValid)            {                return View("Thanks",student);            }            else            {                return View();            }        }
```

If ModelState will be valid then it will display the next view otherwise it will show the same view with [validation error](https://www.mindstick.com/forum/156619/validation-error-in-a-text-field-with-the-help-of-bootstrap) messages.

##### Step 5:

Now add another view to the project to display the values entered by user. To add a view, right click the newly created index() viewresult method in the HomeController and named it “Thanks.cshtml” and make it a strongly – typed view like below:

```
@model ModelValidationApp.Models.Student@{    Layout = null;}<!DOCTYPE html><html><head>    <meta name="viewport" content="width=device-width" />    <title>Thanks</title></head><body>    <div>        <h2>Thank you, @Model.name</h2>    </div></body></html>
```

##### Output

When you run the application.

\
![Model Validation in Asp.net MVC 4](https://www.mindstick.com/blogs/2ae03038-b67d-41b7-939b-5f86bd06ac97/images/709ad627-5f82-4d50-8328-cefb79285381.png)

\

If you click the submit button:-

\
![Model Validation in Asp.net MVC 4](https://www.mindstick.com/blogs/2ae03038-b67d-41b7-939b-5f86bd06ac97/images/adb73f59-8516-4c89-8a4e-c28de91271b0.png)

\

Validation [error messages](https://www.mindstick.com/forum/160245/how-to-customize-error-messages-with-a-data-annotation-in-asp-dot-net-core) will be displayed like above:

Fill the form with the appropriate values and click on [submit button](https://www.mindstick.com/forum/130/its-possible-without-submit-button).

\
![Model Validation in Asp.net MVC 4](https://www.mindstick.com/blogs/2ae03038-b67d-41b7-939b-5f86bd06ac97/images/849036aa-9c8e-4e99-8d68-f8b43deffd02.png)

\

![Model Validation in Asp.net MVC 4](https://www.mindstick.com/blogs/2ae03038-b67d-41b7-939b-5f86bd06ac97/images/2d5cc5e8-9373-4936-a4c5-55f2e8051370.png)

---

Original Source: https://www.mindstick.com/blog/646/model-validation-in-asp-dot-net-mvc-4

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
