aspnetcore/mvc/models/validation.md may be the first page someone reads about validation. Yet it seems to miss the simplest method to return a validation error to the user (where built-in attributes fall short): ModelState.AddModelError.
This method makes it particularly easy, for example, if the validation needs to check a database
if (dbContext.Records.Any(record => record.Name == model.Name))
{
ModelState.AddModelError("Name", "The Name is already in use.");
return View(model);
}
The existing documentation leads the reader to believe that creating a custom ValidationAttribute or implementing IValidatableObject are the only alternatives to built-in attributes.
Thanks for your consideration!
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
Associated WorkItem - 64069
aspnetcore/mvc/models/validation.mdmay be the first page someone reads about validation. Yet it seems to miss the simplest method to return a validation error to the user (where built-in attributes fall short): ModelState.AddModelError.This method makes it particularly easy, for example, if the validation needs to check a database
The existing documentation leads the reader to believe that creating a custom
ValidationAttributeor implementingIValidatableObjectare the only alternatives to built-in attributes.Thanks for your consideration!
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
Associated WorkItem - 64069