MVC3 How to define custom error messages using annotations?

半腔热情 提交于 2019-12-11 08:54:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!