如何在 ASP.NET MVC 中重置验证属性的格式化错误消息?

How to reset the formatted ErrorMessages of the validation attributes in ASP.NET MVC?

我使用了自定义验证属性 -AmountShouldBeLessOrEqualAttribute- 它的验证过程与另一个 属性 的值相关并且此属性有效 成功 .

但在以下情况下我遇到了问题:

  1. 启动应用程序
  2. 前往表单页面
  3. 提交表格(POST表格第一次
  4. ModelBinding 进程导致 AmountShouldBeLessOrEqual 属性中 ErrorMessage 的值被格式化。例如:

    In the ViewModel there is an Amount property with the above attibute

    and

    Its ErrorMessage: Your amount should be less than {0}

    Will be convert to: Your amount should be less than 23

    Note: 23 is the value of MaxAmount property in the ViewModel

  5. 现在我把MaxAmount改成83

  6. 我们再次进入表单页面并提交表单
  7. ModelBinding 进程将启动 AmountShouldBeLessOrEqualAttibute 的验证进程。现在,如果我观察 ErrorMessage 属性 的值,它不是 Your amount should be less than {0},它仍然是旧格式文本:Your amount should be less than 23。所以无法再次格式化为Your amount should be less than 83

我的问题:

我应该如何将格式化后的 ErrorMessages 重置为它的 Non-Formatted 版本以使用新值进行格式化?

ViewModel中:

[AmountShouldBeLessOrEqual(nameof(MaxAmount), ErrorMessage = "Your amount should be less than {0}")]
public decimal Amount { get; set; }

public decimal MaxAmount { get; set; }

AmountShouldBeLessOrEqualAttribute:

public class AmountShouldBeLessOrEqualAttribute : ValidationAttribute
{
    private readonly string _comparisonProperty;
    public AmountShouldBeLessOrEqualAttribute(string comparisonProperty)
    {
        _comparisonProperty = comparisonProperty;
    }
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        ErrorMessage = ErrorMessageString;
        var currentValue = (decimal)value;

        var comparisonValue = GetComparisonValue(_comparisonProperty, validationContext);

        if (ErrorMessage == null && ErrorMessageResourceName == null)
        {
            ErrorMessage = "Amount is large";
        }
        else
        {
            ErrorMessage = string.Format(ErrorMessage ?? "", comparisonValue);
        }

        return currentValue >= comparisonValue
            ? new ValidationResult(ErrorMessage)
            : ValidationResult.Success;
    }

    public override string FormatErrorMessage(string name)
    {
        return base.FormatErrorMessage(name);
    }
    private decimal GetComparisonValue(string comparisonProperty, ValidationContext validationContext)
    {
        var property = validationContext.ObjectType.GetProperty(comparisonProperty);

        if (property == null)
            throw new ArgumentException("Not Found!");

        var comparisonValue = (decimal)property.GetValue(validationContext.ObjectInstance);

        return comparisonValue;
    }
}

这是因为您将 ErrorMessage 的值设置为 string.Format(ErrorMessage ?? "", comparisonValue);ErrorMessage[AmountShouldBeLessOrEqual(nameof(MaxAmount), ErrorMessage = "Your amount should be less than {0}")] 中的值,在 IsValid 期间不应更改。

尝试在IsValid中定义一个作用域变量来存储格式化的错误信息。

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        string error = "";

        var currentValue = (decimal)value;

        var comparisonValue = GetComparisonValue(_comparisonProperty, validationContext);

        if (ErrorMessage == null && ErrorMessageResourceName == null)
        {
            ErrorMessage = "Amount is large";
        }
        else
        {
            error = string.Format(ErrorMessage ?? "", comparisonValue);
        }

        return currentValue >= comparisonValue
            ? new ValidationResult(error)
            : ValidationResult.Success;
    }