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

*爱你&永不变心* 提交于 2019-12-11 15:54:09

问题


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. 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:

    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. Now I change the MaxAmount to 83

  6. We go to the Form page again and submit the form
  7. The ModelBinding process will be start the validation process of AmountShouldBeLessOrEqualAttibute. Now if I watch the value of ErrorMessage property it is not Your amount should be less than {0}, it remained as the old formatted text: Your amount should be less than 23. So it can not be formatted again to Your amount should be less than 83

My question:

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

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

回答1:


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.

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


来源:https://stackoverflow.com/questions/53636422/how-to-reset-the-formatted-errormessages-of-the-validation-attributes-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!