问题
I have a DateTime field(that can accept multiple date time formats, so it is pain to create Regex patter)
When in the field I'm entering something like "Aaaaaa", I'm getting error message:
The value 'Aaaaa' is not valid for OwnerBirthDate
Model looks:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
[Required(ErrorMessage = "*")]
public DateTime? OwnerBirthDate { get; set; }
View:
@Html.TextBoxFor(x => x.OwnerBirthDate)
How can I define custom error message for this particular field?
Thank you
回答1:
You can use ValidationMessageFor to apply a custom message if you want a different message to one pre defined in an attribute
@Html.ValidationMessageFor(m => m.OwnerBirthDate, "custom error message")
来源:https://stackoverflow.com/questions/14397098/mvc3-how-to-define-custom-error-messages-using-annotations