gpt4 book ai didi

c# - 自定义错误消息不出现数据注释

转载 作者:太空狗 更新时间:2023-10-30 00:52:03 25 4
gpt4 key购买 nike

我有一个应用了代码优先数据注释的类,但在 MVC View 中,我发现出现默认错误消息而不是自定义错误消息。请提出任何建议

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; }
}

最佳答案

你需要明确指定一个ErrorMessage

[Required(ErrorMessage = "Email address is required")]
public string EmailAddress { get; set; }

关于c# - 自定义错误消息不出现数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23638895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com