...

/

Handling Validation in Razor Views

Handling Validation in Razor Views

In this section, we will learn how to use validation-specific logic in our views.

We already used validation-specific tag helpers in some of our previous examples. Here, we explain them in detail together with the whole view validation logic.

The overall view validation framework

Views take errors set either by ASP.NET Core providers or set manually by the action method code from the ViewData.ModelState property that is filled with the same ModelStateDictionary used by the controller that rendered the view. Therefore, ViewData.ModelState contains all errors set during the action method execution, each indexed by field name.

Errors are specific to forms since they are associated with the action method triggered by a specific form submit. Thus, if a page contains several forms, our first preoccupation should be that errors are redirected to the tags of the ...