---
title: "How can we do validations in MVC?"  
description: "How can we do validations in MVC?"  
author: "Manoj Bhatt"  
published: 2016-02-07  
updated: 2016-02-07  
canonical: https://www.mindstick.com/forum/33960/how-can-we-do-validations-in-mvc  
category: "asp.net"  
tags: ["c#", "asp.net", "asp.net mvc"]  
reading_time: 2 minutes  

---

# How can we do validations in MVC?

Hi Everyone,Please give me a example, how to [apply validation](https://www.mindstick.com/forum/416/how-to-apply-validation-in-asp-dot-net-mvc3-program) in [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) 4.Can [anyone give me](https://answers.mindstick.com/qa/41194/can-anyone-give-me-some-high-and-low-points-of-each-of-the-four-first-presidential-administrations) a solution.\
\
Thank you.

## Replies

### Reply by Anupam Mishra

We are implementing [validation](https://www.mindstick.com/articles/12234/validation-using-data-annotation-using-entity-framework) in [MVC](https://www.mindstick.com/forum/155803/define-cache-profile-in-mvc) is by using data annotations. Data annotations are nothing but attributes which can be applied on model properties. For example, we have a simple Student class with a property StudentFullName.This StudentFullName property is tagged with a Required data annotation attribute. In other words if this model is not provided Student FullName, it will not accept it.\
\

```
public class Student{    [Required(ErrorMessage=" Student FullName is required")]    public string StudentFullName    {        set;        get;    } }  
```

\
For display of validation error message we need to use the ValidateMessageFor method which belongs to the Html helper class.\

```
@using (Html.BeginForm("StudentFullName", "Home", FormMethod.Post)){ @Html.TextBoxFor(m => m. StudentFullName)@Html.ValidationMessageFor(m => m. StudentFullName).....}
```

\
Later in the controller we can check if the model is proper or not by using the ModelState.IsValid property and accordingly we can take actions.\

```
public ActionResult StudentFullName(Student obj){    if (ModelState.IsValid)    {        obj.Save();        return View("Save Sucessfully");    }    else    {        return View("Student");    }}
```


---

Original Source: https://www.mindstick.com/forum/33960/how-can-we-do-validations-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
