//在部分类中,我试图创建一个自定义的ModelState.IsValid方法。到目前为止,它需要object
作为参数,并接收使用validation属性修饰的所有属性值。这很好,但我想从视图中传递方法另一个参数。然后,我可以使用附加值来帮助确定有效性,并可能根据附加值返回自定义消息。
以下是我尝试过的部分类和一些IsValid方法。
[MetadataType(typeof(CS_Parameter_Statewide_AllGrades_ScenarioMetaData))]
public partial class CS_Parameter_Statewide_AllGrades_Scenario
{
public int Category { get; set; }
public class CS_Parameter_Statewide_AllGrades_ScenarioMetaData
{
[FormatAttribute]
public double Amount { get; set; }
}
}
public class FormatAttribute : ValidationAttribute, IClientValidatable // IClientValidatable for client side Validation
{
// this does not work; i don't know how to pass an additional value
public override bool IsValid(object value, int additonalValue)
{
return true;
}
// this is what I would like to do
public override string IsValid(object value, int additonalValue)
{
if (additonalValue == 1)
// validation method 1
// return message 1
else if (additonalValue == 2)
// validation method 2
// return message 2
else
// validation method 3
// return message 3
}
// this is something I was playing with
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var message = "ohoh";
return new ValidationResult(message);
}
// Implement IClientValidatable for client side Validation
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
return new ModelClientValidationRule[] { new ModelClientValidationRule { ValidationType = "dropdown", ErrorMessage = this.ErrorMessage } };
}
}
`
答案 0 :(得分:2)
您需要一个构造函数来传递属性的名称,该属性的值将包含用于比较和验证的数据。例如
模型
[Format("MyOtherProperty")]
public double Amount { get; set; }
public int MyOtherProperty { get; set; } // the property use to validate
属性
public class FormatAttribute : ValidationAttribute
{
private readonly string _otherProperty;
public FormatAttribute(string otherProperty)
{
_otherProperty = otherProperty;
}
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var property = validationContext.ObjectType.GetProperty(_otherProperty);
if (property == null)
{
return new ValidationResult(string.Format("Unknown property: {0}", _otherProperty));
}
object otherValue = property.GetValue(validationContext.ObjectInstance, null);
if (otherValue == someValue) // cast otherValue to correct type
{
return new ValidationResult("some error message");
}
else if (....)
{
return new ValidationResult("another error message");
}
return null;
}
}
答案 1 :(得分:1)
您可以创建构造函数并在该构造函数中提供其他字段名称
public class FormatAttribute : ValidationAttribute, IClientValidatable // IClientValidatable for client side Validation
{
private string _additionalAttribute;
public FormatAttribute(string additionalAttribut)
{
_additionalAttribute = additionalAttribut
}
}
然后在使用ValidationContext的IsValid函数中,您可以检索附加字段的值
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
object additionalFieldValue = GetPropertyValue(validationContext.ObjectInstance, _additionalField);
// Do Logic Here
}