ModelState.AddModelError() adds an extra validation error when handler-level checks find a business-rule failure that DataAnnotation validation cannot detect. The error appears on the form alongside attribute-based errors. section 16.3.2
Use it during server-side validation
After checking a submitted value against application data or another system, call AddModelError() when the check fails. Then test ModelState.IsValid; if validation failed, return Page() so the form redisplays its errors instead of saving. section 16.3.2
Listing 16.7 Checking model state to view the validation result
public class CheckoutModel : PageModel #1
{
[BindProperty] #2
public UserBindingModel Input { get; set; } #2
public IActionResult OnPost() #3
{
if (!ModelState.IsValid) #4
{
return Page(); #5
}
/* Save to the database, update user, return success */ #6
return RedirectToPage("Success");
}
}
The handler pattern shows how ModelState.IsValid controls whether the form is redisplayed or successful processing continues. section 16.3.2

