【发布时间】:2022-09-23 11:18:41
【问题描述】:
这就是 mvc.net core 3.1 - 这就是我的财产在课堂上的样子
[BindProperty]
[Required(ErrorMessage = \"Enter the valid amount\")]
[ValidDecimal(ErrorMessage = \"Enter the amount correctly\")]
public decimal? QuoteAmountTotal { get; set; }
自定义有效十进制值的代码是
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class ValidDecimalAttribute : ValidationAttribute{
protected override ValidationResult? IsValid(object? value, ValidationContext validationContext)
{
if (value == null || value.ToString().Length == 0)
{
return ValidationResult.Success;
}
return decimal.TryParse(value.ToString(), out _) ? ValidationResult.Success : new ValidationResult(ErrorMessage);
}
}
我在此字段中输入带有空格或字母数字的值。例如 2 0 0 0。 但是,它显示默认的 mvc.net 核心错误,而不是我的自定义错误,即
值 \'2 0 0 0\' 对于 QuoteAmountTotal 无效。
这是 AttemptedvalueisInvalidAccessor enter image description here
我需要显示我的自定义错误消息,而不是默认的 MVC 错误模型消息,在这种情况下不显示。
标签: asp.net-core-mvc modelstate custom-errors error-messaging