Model Validation

Both model binding and model validation occur before the execution of a controller action or a Razor Pages handler method.

Model State

For web apps, it's the app's responsibility to inspect ModelState.IsValid and react appropriately. ModelState represents errors that come from two subsystems: model binding and model validation.

  • Errors that originate from model bindingarrow-up-right are generally data conversion errors. For example, an "x" is entered in an integer field.

  • Model validation occurs after model binding and reports errors where data doesn't conform to business rules. For example, a 0 is entered in a field that expects a rating between 1 and 5.

circle-info

Web API controllers don't have to check ModelState.IsValid if they have the [ApiController] attribute. In that case, an automatic HTTP 400 response containing error details is returned when model state is invalid.

Validation attributes

Last updated