Search⌘ K
AI Features

Validating the Model

Explore how to validate models in ASP.NET Core MVC by applying validation attributes, managing model state errors, and showing error messages in views. Understand how to enhance form data integrity using range, required, and regular expression validations within your website built on the MVC pattern. Practice handling HTTP POST requests and updating views to reflect validation feedback for improved user experience.

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 ...