Validating the Model

Learn about applying validation rules on model state, focusing on validation errors.

The model binding process can cause errors, for example, data type conversions or validation errors, if the model has been decorated with validation rules. What data has been bound, and any binding or validation errors are stored in ControllerBase.ModelState.

Applying validation rules on model state

Let’s explore what we can do with the model state by applying some validation rules to the bound model and then showing invalid data messages in the view:

Step 1: In the Models folder, open Thing.cs.

Step 2: Import the namespace for working with validation attributes, as shown in the following code:

Press + to interact
using System.ComponentModel.DataAnnotations;

...