---
title: "How to manually check if model is valid or not"  
description: "How to manually check if model is valid or not"  
author: "Anonymous User"  
published: 2018-04-29  
updated: 2023-06-18  
canonical: https://www.mindstick.com/forum/34478/how-to-manually-check-if-model-is-valid-or-not  
category: "asp.net mvc"  
tags: ["asp.net", "asp.net mvc"]  
reading_time: 2 minutes  

---

# How to manually check if model is valid or not

I want to [check the data](https://www.mindstick.com/forum/161517/how-do-you-check-the-data-type-of-a-variable) is valid or not

```
 public ActionResult ValidCheck(student model)
    {

        Models.ViewModel.User model = new Models.ViewModel.User();
        model.Age = 12;
        model.Name = "Andy He";
        model.Email = "123";
        //TryValidateModel(model);
     }
```

could you help me?

## Replies

### Reply by Aryan Kumar

Sure, here are the steps on how to manually [check](https://yourviews.mindstick.com/story/2248/never-forget-to-check-these-specifications-before-buying-a-mobile-phone) if a model is valid or not in MVC:

1. Get the ModelState object from the controller action.
2. Check the ModelState.IsValid property.
3. If the ModelState.IsValid property is false, then the model is not valid.
4. Otherwise, the model is valid.

Here is an example of how to manually check if a model is valid or not in MVC:

C#

```plaintext
public class IndexModel
{
  public string Name { get; set; }
}

public class IndexController : Controller
{
  public ActionResult Index()
  {
    // Get the model.
    var model = new IndexModel();

    // Check the model validity.
    if (!ModelState.IsValid)
    {
      // The model is not valid.
      return View(model);
    }

    // The model is valid.
    return RedirectToAction("Success");
  }
}
```

This code will create a controller action that checks the validity of the IndexModel model. If the model is not valid, then the action will render the Index view. Otherwise, the action will redirect to the Success action.

The ModelState object is a dictionary that contains the validation errors for the model. The ModelState.IsValid property is a Boolean property that indicates whether the model is valid or not. If the ModelState.IsValid property is true, then the model is valid. Otherwise, the model is not valid.

### Reply by Anonymous User

```
 public ActionResult ValidCheck(student model)    {
if(!ModelState.IsValid)
{

        Models.ViewModel.User model = new Models.ViewModel.User();
        model.Age = 12;
        model.Name = "Andy He";
        model.Email = "123";
        //TryValidateModel(model);
}
     }
```

\

try this code hope it resolve your problem

\


---

Original Source: https://www.mindstick.com/forum/34478/how-to-manually-check-if-model-is-valid-or-not

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
