liveBook · Manning

Validation summary

As explained in ASP.NET Core in Action, 3e

Validation Tag Helpers are ASP.NET Core Tag Helpers that render model-validation errors in Razor forms.

A validation summary is a Razor <div> that displays validation errors collected in ModelState. It complements property-level messages by giving users one place to find form-wide and field-specific problems. section 18.2.5 section 18.6

Apply the Validation Summary Tag Helper with asp-validation-summary:

  • None displays no summary. section 18.2.5
  • ModelOnly displays errors not associated with a specific property. section 18.2.5
  • All displays both property errors and model-level errors. section 18.2.5
  • Keep asp-validation-for spans near individual inputs when users need precise field feedback. section 18.2.5

Showing a model-level error

A currency-conversion form can add an error with ModelState.AddModelError(string.Empty, "Cannot convert currency to itself"). Returning the page when ModelState.IsValid is false lets a summary configured with All display that error, even though it belongs to the model rather than one input. section 18.2.5

<div asp-validation-summary="All"></div>

Use <div asp-validation-summary="All"></div> to render the summary as a list of ModelState errors. section 18.2.5

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains the three ValidationSummary values and demonstrates model-level ModelState errors, but defers broader ModelState details.
Examples
Uses a currency converter error and the markup <div asp-validation-summary="All"></div>.
What it assumes
Assumes basic Razor forms and model validation while introducing ModelState errors and ValidationSummary values.
What you can do after
Apply it via Razor Tag Helpers to expose property and model-level validation errors to form users.
How it connects
Connects ModelState, server-side validation, client-side validation, Razor forms, and Validation Message Tag Helpers.

Also explained in

Compare how 3 books explain validation summary →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.