本文介绍了如何在ASP.NET MVC中重置验证属性的格式化ErrorMessages?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了自定义"验证属性-AmountShouldBeLessOrEqualAttribute-,该属性的验证过程与另一个属性的值有关,并且此属性成功起作用.

I used a Custom validation attribute -AmountShouldBeLessOrEqualAttribute- that its validation process related to value of another property and this attribute works successfully.

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

But in the following scenario I have a problem with it:

  1. 启动应用程序
  2. 转到表单"页面
  3. 提交表单(首次发布表单 )
  4. ModelBinding进程导致AmountShouldBeLessOrEqual属性中的ErrorMessage的值被格式化.例如:

  1. Start the Application
  2. Going to the Form page
  3. Submit the Form (POST the form for first time)
  4. The ModelBinding process cause that the value of ErrorMessage in the AmountShouldBeLessOrEqual attribute be formatted. For example:

其错误消息:Your amount should be less than {0}

将被转换为:Your amount should be less than 23

注意:23ViewModel

  • 现在我将MaxAmount更改为83

  • Now I change the MaxAmount to 83

    我的问题:

    每次使用新值进行格式化时,应如何将格式化的ErrorMessages重置为其Non-Formatted版本?

    How should I reset the formatted ErrorMessages to its Non-Formatted version each time to be formatted with new value?

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

    推荐答案

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

    This is caused that you are setting value for ErrorMessage with string.Format(ErrorMessage ?? "", comparisonValue);. ErrorMessage is the value from [AmountShouldBeLessOrEqual(nameof(MaxAmount), ErrorMessage = "Your amount should be less than {0}")] which you should not change during IsValid.

    尝试在IsValid中定义作用域变量以存储格式化的错误消息.

    Try to define a scoped variable in IsValid to store the formatted error message.

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

    这篇关于如何在ASP.NET MVC中重置验证属性的格式化ErrorMessages?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-05 22:24