MVC3如何使用注释定义自定义错误消息? [英] MVC3 How to define custom error messages using annotations?

查看:171
本文介绍了MVC3如何使用注释定义自定义错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DateTime场(即可以接受多个日期和时间格式,所以它是痛苦创建正则表达式图案)

I have a DateTime field(that can accept multiple date time formats, so it is pain to create Regex patter)

当我进入类似AAAAAA,我得到错误信息字段:

When in the field I'm entering something like "Aaaaaa", I'm getting error message:

The value 'Aaaaa' is not valid for OwnerBirthDate

模型如下:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
[Required(ErrorMessage = "*")]
public DateTime? OwnerBirthDate { get; set; }

查看:

@Html.TextBoxFor(x => x.OwnerBirthDate)

如何定义自定义错误消息为这一特定领域?

How can I define custom error message for this particular field?

感谢您

推荐答案

您可以使用ValidationMessageFor如果你想要一个不同的消息,在属性定义了一个pre应用自定义消息

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")

这篇关于MVC3如何使用注释定义自定义错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆