问题描述
我使用了自定义"验证属性-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:
- 启动应用程序
- 转到表单"页面
- 提交表单(首次发布表单 )
-
ModelBinding
进程导致AmountShouldBeLessOrEqual
属性中的ErrorMessage
的值被格式化.例如:
- Start the Application
- Going to the Form page
- Submit the Form (POST the form for first time)
The
ModelBinding
process cause that the value ofErrorMessage
in theAmountShouldBeLessOrEqual
attribute be formatted. For example:
和
其错误消息:Your amount should be less than {0}
将被转换为:Your amount should be less than 23
注意:23
是ViewModel
现在我将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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!