MVC3 How to define custom error messages using ann

2019-08-14 16:42发布

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条回答
Rolldiameter
2楼-- · 2019-08-14 16:56

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")
查看更多
登录 后发表回答