相关疑难解决方法(0)

我如何使用IValidatableObject?

据我所知,IValidatableObject它用于以一种方式验证对象,让人们相互比较属性.

我仍然希望有属性来验证单个属性,但我想在某些情况下忽略某些属性的失败.

我是否试图在下面的情况下错误地使用它?如果不是我如何实现这个?

public class ValidateMe : IValidatableObject
{
    [Required]
    public bool Enable { get; set; }

    [Range(1, 5)]
    public int Prop1 { get; set; }

    [Range(1, 5)]
    public int Prop2 { get; set; }

    public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
    {
        if (!this.Enable)
        {
            /* Return valid result here.
             * I don't care if Prop1 and Prop2 are out of range
             * if the whole object is not "enabled"
             */
        }
        else
        {
            /* Check if Prop1 and Prop2 …
Run Code Online (Sandbox Code Playgroud)

c# asp.net ivalidatableobject

163
推荐指数
4
解决办法
11万
查看次数

标签 统计

asp.net ×1

c# ×1

ivalidatableobject ×1