カスタムの検証属性を作成する必要があります。Web には多くのヘルプがあります。以下は、同様の依存属性からの適応です。
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;
}
}
ここで行う必要があるのは、カスタム属性の例を見つけて、上記を使用するように調整することです。
健康に関する警告 - これはまったくテストされておらず、おそらくエラーが含まれています。
幸運を!