METHOD

ModelStateDictionary.AddModelError

Records a validation error under a field or model key. Retain prior binding errors and check IsValid before any mutation. This does not execute the MVC pipeline.

Formal Syntax & Structure

csharp syntaxcsharp
state.AddModelError("Quantity", "Use 1 through 5.");

Working Example

Example Snippetcsharp
using System;using Microsoft.AspNetCore.Mvc.ModelBinding;var state = new ModelStateDictionary();state.AddModelError("Quantity", "Use 1 through 5.");Console.WriteLine(state.IsValid); // False
C# & .NET Reference