gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:11:56 26 4
gpt4 key购买 nike

我正在尝试创建一个自定义验证,它表示“如果 otherValue 为真,则此值必须大于 0。我能够获取值,但我目前设置 otherValue 的方式,我只有属性的名称,而不是值。可能是因为它作为字符串传入。这个属性将出现在 5 或 6 个不同的属性上,并且每次都会调用不同的 otherValue。寻求帮助如何获取 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;
}

}

所以我应该能够做到这一点:

    [MustBeGreaterIfTrue(OtherProperty="EverHadRestrainingOrder", ErrorMessage="Enter more info on your RO.")]
public int? ROCounter { get; set; }

最佳答案

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");
}
}

示例用法:

public class SomeModel
{
[MustBeGreaterIf(OtherPropertyName="Prop2")]
public string Prop1 {get;set;}
public string Prop2 {get;set;}
}

引用:http://www.concurrentdevelopment.co.uk/blog/index.php/2011/01/custom-validationattribute-for-comparing-properties/

关于c# - 获取作为字符串传入的属性的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13071219/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com