C# 要求使用枚举进行数据批注

C# 要求使用枚举进行数据批注,c#,asp.net-mvc,data-annotations,C#,Asp.net Mvc,Data Annotations,我创建了一个自定义RequiredIf验证器,如下所示: public class RequiredIfValidator : ValidationAttribute, IClientValidatable { RequiredAttribute _innerAttribute = new RequiredAttribute(); public string _dependentProperty { get; set; } public object _targetVal

我创建了一个自定义RequiredIf验证器,如下所示:

public class RequiredIfValidator : ValidationAttribute, IClientValidatable
{
    RequiredAttribute _innerAttribute = new RequiredAttribute();
    public string _dependentProperty { get; set; }
    public object _targetValue { get; set; }

    public RequiredIfValidator(string dependentProperty, object targetValue)
    {
        this._dependentProperty = dependentProperty;
        this._targetValue = targetValue;
    }
    public override string FormatErrorMessage(string name)
    {
        return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, _dependentProperty);
    }
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var field = validationContext.ObjectInstance.GetType().GetProperty(_dependentProperty);
        if (field != null)
        {
            var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
            if ((dependentValue == null && _targetValue == null) ||(dependentValue.Equals(_targetValue)))
            {
                if (!_innerAttribute.IsValid(value))
                {
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
                }
            }
        }
        return ValidationResult.Success;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule();
        rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
        rule.ValidationType = "requiredif";
        rule.ValidationParameters["dependentproperty"] = _dependentProperty;
        rule.ValidationParameters["targetvalue"] = _targetValue;
        yield return rule;
    }
}
public enum TestTypes 
{
    Hair = 1,
    Urine = 2
}
public class TestViewModel
{
    public TestTypes TestTypeId {get; set;}

    [RequiredIfValidator("TestTypeId", TestTypes.Hair)]
    public string HairSpecimenId {get; set;}
}
我的ViewModel具有如下属性:

public class RequiredIfValidator : ValidationAttribute, IClientValidatable
{
    RequiredAttribute _innerAttribute = new RequiredAttribute();
    public string _dependentProperty { get; set; }
    public object _targetValue { get; set; }

    public RequiredIfValidator(string dependentProperty, object targetValue)
    {
        this._dependentProperty = dependentProperty;
        this._targetValue = targetValue;
    }
    public override string FormatErrorMessage(string name)
    {
        return string.Format(CultureInfo.CurrentCulture, ErrorMessageString, name, _dependentProperty);
    }
    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
        var field = validationContext.ObjectInstance.GetType().GetProperty(_dependentProperty);
        if (field != null)
        {
            var dependentValue = field.GetValue(validationContext.ObjectInstance, null);
            if ((dependentValue == null && _targetValue == null) ||(dependentValue.Equals(_targetValue)))
            {
                if (!_innerAttribute.IsValid(value))
                {
                    return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
                }
            }
        }
        return ValidationResult.Success;
    }

    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule();
        rule.ErrorMessage = FormatErrorMessage(metadata.GetDisplayName());
        rule.ValidationType = "requiredif";
        rule.ValidationParameters["dependentproperty"] = _dependentProperty;
        rule.ValidationParameters["targetvalue"] = _targetValue;
        yield return rule;
    }
}
public enum TestTypes 
{
    Hair = 1,
    Urine = 2
}
public class TestViewModel
{
    public TestTypes TestTypeId {get; set;}

    [RequiredIfValidator("TestTypeId", TestTypes.Hair)]
    public string HairSpecimenId {get; set;}
}

我的自定义要求验证程序不在该scinario中工作。是因为枚举数据类型吗?使用枚举实现这一点的任何方法
IsValid()
中的逻辑似乎都不正确。应该是

protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
  if (value == null)
  {
    var otherProperty = validationContext.ObjectInstance.GetType().GetProperty(_dependentProperty);
    var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);
    if (otherPropertyValue != null && otherPropertyValue.Equals(_targetValue ))
    {
      return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }
  }
  return ValidationResult.Success;
}

如何生成枚举的值?您是否使用下拉列表,如果是,每个选项的
属性是什么(例如
1
Hair
)?不,它不是下拉列表。我刚刚在这里展示了一个样品。这就像相同的视图模型类和相同的局部视图UI被用于头发和尿液测试。如果是头发,则需要HairSpecimenId,否则不需要。我正在控制器中将这个TestTypeId初始化为TestTypes.Hair或TestTypes.Urine。那么
RequiredAttribute\u innerAttribute=new RequiredAttribute()的目的是什么?这是不必要的