In the same way that you can add a model validation within the controller:

ModelState.AddValidationError([KEY], "This is an error message")

Can I add the error within my model validation using annotations?

// For demonstration purposes only
[Required(ErrorMessage = "This is an error message"), AddErrorMessageToKey = [KEY]]
public string UserName { get; set; }

Sometimes it's not always appropriate to add the error message to a particular property. In my application I wish to only show one error message.

Related posts

Recent Viewed