If you are programming in ASP.NET 5.0 MVC, you will certainly have seen the class ModelState pop up a few times. Ever wondered what that is, or what the property ModelState.IsValid does, and why it’s there? So did I.

In this post, we’re going to explain what the ModelState is, and what it is used for. We’ll also show how to use it to validate our POSTed inputs, and do simple custom validation. Let’s go!

What is the ModelState?

In short, the ModelState is a collection of name and value pairs that are submitted to the server during a POST. It also contains error messages about each name-value pair, if any are found.

ModelState is a property of a Controller instance, and can be accessed from any class that inherits from Microsoft.AspNetCore.Mvc.Controller.

The ModelState has two purposes: to store and submit POSTed name-value pairs, and to store the validation errors associated with each value.

All right, enough of the boring explanation. It’s code time!

#aspdotnet 5.0 mvc #aspdotnet mvc #modelstate

Explaining the ModelState in ASP.NET 5.0 MVC
1.90 GEEK