Question

So, that I may call ModelState.IsValid in a controller ?

What is checked ? What is it checked against ? Where is it happening ?

I had a look at the MSDN doc's and it didn't reveal much.

Was it helpful?

Solution

What is checked ?

The ModelState dictionary.

What is it checked against ?

Whether this dictionary contains an element with an error.

Where is it happening ?

Whenever you call the IsValid method. But maybe you are asking when is this dictionary populated. It is populated by the default model binder. For example let's suppose that you have the following controller action:

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    if (ModelState.IsValid) ...
}

When the default model binder is binding to the view model (before the controller action executes), it will apply the necessary validation when parsing the requests values and if there are errors, it will add them to the ModelState dictionary. So, once the code reaches the controller action and you check the IsValid property, this dictionary is already populated.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top