# Model Validation

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

## 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.&#x20;

* Errors that originate from [model binding](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-6.0) are generally data conversion errors. \
  For example, an "x" is entered in an integer field.&#x20;
* 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.

{% hint style="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.
{% endhint %}

### Validation attributes <a href="#validation-attributes" id="validation-attributes"></a>



* [\[ValidateNever\]](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.modelbinding.validation.validateneverattribute): Indicates that a property or parameter should be excluded from validation.
* [\[CreditCard\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.creditcardattribute): Validates that the property has a credit card format. Requires [jQuery Validation Additional Methods](https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.1/additional-methods.js).
* [\[Compare\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.compareattribute): Validates that two properties in a model match.
* [\[EmailAddress\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.emailaddressattribute): Validates that the property has an email format.
* [\[Phone\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.phoneattribute): Validates that the property has a telephone number format.
* [\[Range\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.rangeattribute): Validates that the property value falls within a specified range.
* [\[RegularExpression\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.regularexpressionattribute): Validates that the property value matches a specified regular expression.
* [\[Required\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.requiredattribute): Validates that the field isn't null. See [`[Required]` attribute](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#non-nullable-reference-types-and-required-attribute) for details about this attribute's behavior.
* [\[StringLength\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.stringlengthattribute): Validates that a string property value doesn't exceed a specified length limit.
* [\[Url\]](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.urlattribute): Validates that the property has a URL format.
* [\[Remote\]](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.remoteattribute): Validates input on the client by calling an action method on the server. See [`[Remote]` attribute](https://learn.microsoft.com/en-us/aspnet/core/mvc/models/validation?view=aspnetcore-6.0#remote-attribute) for details about this attribute's behavior.
