您需要创建一个自定义验证属性 - 网络上有很多帮助。以下是对类似依赖属性的改编。
public class GreaterThanOtherAttribute : ValidationAttribute, IClientValidatable
{
public string DependentProperty { get; set; }
public GreaterThanOtherAttribute (string dependentProperty)
{
this.DependentProperty = dependentProperty;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
// get a reference to the property this validation depends upon
var containerType = validationContext.ObjectInstance.GetType();
var field = containerType.GetProperty(this.DependentProperty);
if (field != null)
{
// get the value of the dependent property
var dependentvalue = field.GetValue(validationContext.ObjectInstance, null);
// compare the value against the target value
if ((dependentvalue == null && this.TargetValue == null) ||
(dependentvalue != null && dependentvalue < this.TargetValue)))
{
// match => means we should try validating this field
return new ValidationResult(this.ErrorMessage, new[] { validationContext.MemberName });
}
}
return ValidationResult.Success;
}
然后装饰你的模型:
public class id
{
[Required]
public decimal l_ID
{
get;
set;
}
[Required]
[GreaterThanOtherAttribute("l_ID")]
public decimal v_ID
{
get;
set;
}
}
您现在需要做的是找到一个示例自定义属性并对其进行调整以使用上述内容。
健康警告 - 未经任何测试,可能包含错误。
祝你好运!