Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取作为字符串传入的属性的值_C#_Asp.net Mvc_Validation - Fatal编程技术网

C# 获取作为字符串传入的属性的值

C# 获取作为字符串传入的属性的值,c#,asp.net-mvc,validation,C#,Asp.net Mvc,Validation,我正在尝试创建一个自定义验证,该验证说明如果otherValue为true,则该值必须大于0。我可以在中获取值,但按照当前设置otherValue的方式,我只有属性的名称,而没有值。可能是因为它作为字符串传入。该属性将位于5个或6个不同的属性上,每次都将调用不同的otherValue。寻求关于如何获得实际值的帮助,这是一个其他值的bool 这是我目前的代码: public class MustBeGreaterIfTrueAttribute : ValidationAttribute, ICli

我正在尝试创建一个自定义验证,该验证说明如果otherValue为true,则该值必须大于0。我可以在中获取值,但按照当前设置otherValue的方式,我只有属性的名称,而没有值。可能是因为它作为字符串传入。该属性将位于5个或6个不同的属性上,每次都将调用不同的otherValue。寻求关于如何获得实际值的帮助,这是一个其他值的bool

这是我目前的代码:

public class MustBeGreaterIfTrueAttribute : ValidationAttribute, IClientValidatable
{
    // get the radio button value
    public string OtherValue { get; set; }

    public override bool IsValid(object value)
    {
        // Here is the actual custom rule
        if (value.ToString() == "0")
        {
            if (OtherValue.ToString() == "true")
            {
                return false;
            }
        }
        // If all is ok, return successful.
        return true;
    }
===============================编辑=========================

这就是我现在所处的位置,而且很有效!现在,我需要一个关于如何创建它的参考,以便在模型中添加属性时可以输入不同的错误消息:

public class MustBeGreaterIfTrueAttribute : ValidationAttribute, IClientValidatable
{
    // get the radio button value
    public string OtherProperty { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext context)
    {
        var otherPropertyInfo = context.ObjectInstance.GetType();
        var otherValue = otherPropertyInfo.GetProperty(OtherProperty).GetValue(context.ObjectInstance, null);      

        // Here is the actual custom rule
        if (value.ToString() == "0")
        {
            if (otherValue.ToString().Equals("True", StringComparison.InvariantCultureIgnoreCase))
            {
                return new ValidationResult("Ensure all 'Yes' answers have additional data entered.");
            }
        }
        // If all is ok, return successful.
        return ValidationResult.Success;
    }

    // Add the client side unobtrusive 'data-val' attributes
    public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    {
        var rule = new ModelClientValidationRule();
        rule.ValidationType = "requiredifyes";
        rule.ErrorMessage = this.ErrorMessage;
        rule.ValidationParameters.Add("othervalue", this.OtherProperty);
        yield return rule;
    }

}

我不太明白你想做什么但是

如果您想获得OtherValue的布尔表示,可以这样做

bool.Parse(this.OtherValue);
此外,对于字符串比较,有时可以从图片中去掉大小写和空格

if (this.OtherValue.ToString().ToLower().Trim() == "true")

我不太明白你想做什么但是

如果您想获得OtherValue的布尔表示,可以这样做

bool.Parse(this.OtherValue);
此外,对于字符串比较,有时可以从图片中去掉大小写和空格

if (this.OtherValue.ToString().ToLower().Trim() == "true")
ValidationAttribute有一对IsValid方法,对于您的场景,您必须使用其他方法

  public class MustBeGreaterIfTrueAttribute : ValidationAttribute
  {
    // name of the OtherProperty. You have to specify this when you apply this attribute
    public string OtherPropertyName { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
      var otherProperty = validationContext.ObjectType.GetProperty(OtherPropertyName);

      if (otherProperty == null)
        return new ValidationResult(String.Format("Unknown property: {0}.", OtherPropertyName));

      var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);

      if (value.ToString() == "0")
      {
        if (otherPropertyValue != null && otherPropertyValue.ToString() == "true")
        {
          return null;
        }
      }

      return new ValidationResult("write something here");
    }
  }
用法示例:

Ref:

ValidationAttribute有一对IsValid方法,对于您的场景,您必须使用其他方法

  public class MustBeGreaterIfTrueAttribute : ValidationAttribute
  {
    // name of the OtherProperty. You have to specify this when you apply this attribute
    public string OtherPropertyName { get; set; }

    protected override ValidationResult IsValid(object value, ValidationContext validationContext)
    {
      var otherProperty = validationContext.ObjectType.GetProperty(OtherPropertyName);

      if (otherProperty == null)
        return new ValidationResult(String.Format("Unknown property: {0}.", OtherPropertyName));

      var otherPropertyValue = otherProperty.GetValue(validationContext.ObjectInstance, null);

      if (value.ToString() == "0")
      {
        if (otherPropertyValue != null && otherPropertyValue.ToString() == "true")
        {
          return null;
        }
      }

      return new ValidationResult("write something here");
    }
  }
用法示例:


Ref:

validationContext具有ObjectInstance,它是某个模型类的实例,因此您可以使用:
validationContext.ObjectInstance作为SomeModel。Prop1/Prop2

validationContext具有ObjectInstance,它是SomeModel类的实例,因此您可以使用:
validationContext.ObjectInstance作为SomeModel.Prop1/Prop2

为什么在OtherValue已定义为字符串时调用OtherValue.ToString?好的,我将停止这样做。为什么在OtherValue已定义为字符串时调用OtherValue.ToString?好的,我将停止这样做。