ASP.NET MVC-选择要使用的验证批注

ASP.NET MVC-选择要使用的验证批注,asp.net,asp.net-mvc,validation,data-annotations,Asp.net,Asp.net Mvc,Validation,Data Annotations,我有一个具有如下属性的模型: public class YourDetails { [Required(ErrorMessage = "Code is required")] [StringLength(10, ErrorMessage = "Code length is wrong", MinimumLength = 2)] [Range(0, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")

我有一个具有如下属性的模型:

public class YourDetails {

  [Required(ErrorMessage = "Code is required")]
  [StringLength(10, ErrorMessage = "Code length is wrong", MinimumLength = 2)]
  [Range(0, int.MaxValue, ErrorMessage = "Please enter a value bigger than {1}")]
  public int Code { get; set; }

}
用户界面验证是通过不引人注目的JS验证插件以开箱即用的方式设置的

问题:我有两个导航动作,返回和下一个。下一步是好的,当事情出错时,验证启动,当事情正确时,即
.isValid()
返回true,数据将传递给数据库服务等

但是,当我按下“后退”键时,我需要在保存之前以不同方式验证表单/视图模型。也就是说,确保
code
是一个正整数,但不要麻烦进行
所需的
StringLength
验证


所以基本上,我想在下一步完全验证,但在后面部分验证。这可能吗?

当我在过去做过类似的事情时,我发现最简单的方法就是使用流畅的验证。您可以将参数传递给验证器并切换到不同的规则集。

当我过去做过类似的事情时,我发现最简单的方法就是使用流畅的验证。您可以将参数传递给验证器并切换到不同的规则集。

我过去使用过以下条件“Required”和“StringLength”属性,它们工作得很好

如果属性为:

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
    public class RequiredIfAttribute : RequiredAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public RequiredIfAttribute(string otherProperty, object value)
            : base()
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        private object _TypeId = new object();

        public override object TypeId
        {
            get
            {
                return _TypeId;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (IsRequired(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                ValidationResult isValid = base.IsValid(value, validationContext);
                return isValid;
            }
            return ValidationResult.Success;
        }

        protected virtual bool IsRequired(object otherPropertyValue, object actualOtherPropertyValue)
        {
            return object.Equals(OtherPropertyValue, actualOtherPropertyValue);
        }
    }
}
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    public class StringLengthIfAttribute : StringLengthAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public StringLengthIfAttribute(int maximumLength, string otherProperty, object value)
            : base(maximumLength)
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (object.Equals(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                return base.IsValid(value, validationContext);
            }
            return null;
        }
    }
}
public class MyModel
{
    [RequiredIf("IsBack", false)]
    public string Name { get; set; }

    public bool IsBack { get; set; } 
}
如果属性:

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
    public class RequiredIfAttribute : RequiredAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public RequiredIfAttribute(string otherProperty, object value)
            : base()
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        private object _TypeId = new object();

        public override object TypeId
        {
            get
            {
                return _TypeId;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (IsRequired(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                ValidationResult isValid = base.IsValid(value, validationContext);
                return isValid;
            }
            return ValidationResult.Success;
        }

        protected virtual bool IsRequired(object otherPropertyValue, object actualOtherPropertyValue)
        {
            return object.Equals(OtherPropertyValue, actualOtherPropertyValue);
        }
    }
}
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    public class StringLengthIfAttribute : StringLengthAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public StringLengthIfAttribute(int maximumLength, string otherProperty, object value)
            : base(maximumLength)
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (object.Equals(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                return base.IsValid(value, validationContext);
            }
            return null;
        }
    }
}
public class MyModel
{
    [RequiredIf("IsBack", false)]
    public string Name { get; set; }

    public bool IsBack { get; set; } 
}
用法示例:

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
    public class RequiredIfAttribute : RequiredAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public RequiredIfAttribute(string otherProperty, object value)
            : base()
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        private object _TypeId = new object();

        public override object TypeId
        {
            get
            {
                return _TypeId;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (IsRequired(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                ValidationResult isValid = base.IsValid(value, validationContext);
                return isValid;
            }
            return ValidationResult.Success;
        }

        protected virtual bool IsRequired(object otherPropertyValue, object actualOtherPropertyValue)
        {
            return object.Equals(OtherPropertyValue, actualOtherPropertyValue);
        }
    }
}
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    public class StringLengthIfAttribute : StringLengthAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public StringLengthIfAttribute(int maximumLength, string otherProperty, object value)
            : base(maximumLength)
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (object.Equals(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                return base.IsValid(value, validationContext);
            }
            return null;
        }
    }
}
public class MyModel
{
    [RequiredIf("IsBack", false)]
    public string Name { get; set; }

    public bool IsBack { get; set; } 
}

我在过去使用过以下条件“Required”和“StringLength”属性,它们工作得很好

如果属性为:

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
    public class RequiredIfAttribute : RequiredAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public RequiredIfAttribute(string otherProperty, object value)
            : base()
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        private object _TypeId = new object();

        public override object TypeId
        {
            get
            {
                return _TypeId;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (IsRequired(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                ValidationResult isValid = base.IsValid(value, validationContext);
                return isValid;
            }
            return ValidationResult.Success;
        }

        protected virtual bool IsRequired(object otherPropertyValue, object actualOtherPropertyValue)
        {
            return object.Equals(OtherPropertyValue, actualOtherPropertyValue);
        }
    }
}
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    public class StringLengthIfAttribute : StringLengthAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public StringLengthIfAttribute(int maximumLength, string otherProperty, object value)
            : base(maximumLength)
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (object.Equals(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                return base.IsValid(value, validationContext);
            }
            return null;
        }
    }
}
public class MyModel
{
    [RequiredIf("IsBack", false)]
    public string Name { get; set; }

    public bool IsBack { get; set; } 
}
如果属性:

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
    public class RequiredIfAttribute : RequiredAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public RequiredIfAttribute(string otherProperty, object value)
            : base()
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        private object _TypeId = new object();

        public override object TypeId
        {
            get
            {
                return _TypeId;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (IsRequired(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                ValidationResult isValid = base.IsValid(value, validationContext);
                return isValid;
            }
            return ValidationResult.Success;
        }

        protected virtual bool IsRequired(object otherPropertyValue, object actualOtherPropertyValue)
        {
            return object.Equals(OtherPropertyValue, actualOtherPropertyValue);
        }
    }
}
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    public class StringLengthIfAttribute : StringLengthAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public StringLengthIfAttribute(int maximumLength, string otherProperty, object value)
            : base(maximumLength)
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (object.Equals(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                return base.IsValid(value, validationContext);
            }
            return null;
        }
    }
}
public class MyModel
{
    [RequiredIf("IsBack", false)]
    public string Name { get; set; }

    public bool IsBack { get; set; } 
}
用法示例:

using System;
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)]
    public class RequiredIfAttribute : RequiredAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public RequiredIfAttribute(string otherProperty, object value)
            : base()
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        private object _TypeId = new object();

        public override object TypeId
        {
            get
            {
                return _TypeId;
            }
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (IsRequired(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                ValidationResult isValid = base.IsValid(value, validationContext);
                return isValid;
            }
            return ValidationResult.Success;
        }

        protected virtual bool IsRequired(object otherPropertyValue, object actualOtherPropertyValue)
        {
            return object.Equals(OtherPropertyValue, actualOtherPropertyValue);
        }
    }
}
using System.ComponentModel.DataAnnotations;
using System.Reflection;

namespace Website.Core.Mvc.DataAnnotations
{
    public class StringLengthIfAttribute : StringLengthAttribute
    {
        public string OtherProperty { get; set; }

        public object OtherPropertyValue { get; set; }

        public StringLengthIfAttribute(int maximumLength, string otherProperty, object value)
            : base(maximumLength)
        {
            OtherProperty = otherProperty;
            OtherPropertyValue = value;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.OtherProperty);
            if (property == null)
            {
                return new ValidationResult(this.OtherProperty + " not found");
            }

            // Get 
            object actualOtherPropertyValue = property.GetValue(validationContext.ObjectInstance, null);

            // If the other property matches the expected value then validate as normal
            if (object.Equals(OtherPropertyValue, actualOtherPropertyValue))
            {
                // Call base and validate required as normal
                return base.IsValid(value, validationContext);
            }
            return null;
        }
    }
}
public class MyModel
{
    [RequiredIf("IsBack", false)]
    public string Name { get; set; }

    public bool IsBack { get; set; } 
}

我认为这是最好的办法。看起来,使用Fluent,您可以连接到客户端JS验证,并选择何时进行验证。美好的谢谢。我认为这是最好的方法。看起来,使用Fluent,您可以连接到客户端JS验证,并选择何时进行验证。美好的谢谢