我有一个应用代码第一个数据注释的类,但在MVC视图中,我发现出现默认错误消息而不是自定义错误消息。任何ssugesstion请
public class PaymentModel
{
[DisplayName("Email Address")]
[Required]
[RegularExpression(@"^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$", ErrorMessage = "Please enter a valid Email Address")]
public string EmailAddress { get; set; }
[DisplayName("Credit Card Number")]
[Required]
[StringLength(16, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 3)]
public int CreditCardNumber { get; set; }
[Required]
[StringLength(3, ErrorMessage = "The {0} must be at least {1} characters long.", MinimumLength = 3)]
public int Cvv { get; set; }
[DisplayName("Expiry Month")]
[Required]
public int ExpiryMonth { get; set; }
[DisplayName("Expiry Year")]
[Required]
public int ExpiryYear { get; set; }
}
答案 0 :(得分:6)
您需要明确指定ErrorMessage
[Required(ErrorMessage = "Email address is required")]
public string EmailAddress { get; set; }