I enjoy writing validation functions in my controller that modify the ModelState
if the validation fails. For example:
private bool ValidateMoney(string raw, string name, decimal min, decimal max) {
try {
var dec = Convert.ToDecimal(raw);
if (dec < min) {
throw new ArgumentOutOfRangeException(name + " must be >= " + min);
}
else if (dec > max) {
throw new ArgumentOutOfRangeException(name + " must be <= " + max);
}
}
catch (Exception ex) {
ModelState.AddModelError(name, ex.GetUserMessage());
}
return ModelState.IsValid;
}
But, I never know what to put for that stupid "key" argument to ModelState.AddModelError
. (In the example, I just set it to my UI display name.)
What were the MVC developers thinking when they added it?