2013-04-04 37 views
1

我有看起来像这种性质的模型,验证注释:ASP.NET MVC - 选择使用

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

} 

的UI验证设置通常的开箱即用的方式与不显眼的JS验证插件。

问题:我有2个导航操作,返回和下一个。接下来是好的,当事情是错误的,验证火灾,当事情是正确的,即.isValid()返回true,数据被传递给数据库服务等等

但是,当我按'返回'我有一个要求,以验证形式/ ViewModel不同之前保存。即确保Code是一个正整数,但不要打扰RequiredStringLength验证。

所以基本上我想在Next上完全验证,但部分在Back上验证。那可能吗?

回答

1

当我在过去做过类似的事情时,我发现的最简单的方法是使用流利的验证http://fluentvalidation.codeplex.com/wikipage?title=mvc。您可以将参数传递给验证程序并切换到不同的规则集。

+1

我觉得这是个最好的方法。似乎用Fluent你可以挂钩到客户端的JS验证,并选择什么时候验证。尼斯。谢谢。 – Greg 2013-04-05 08:08:48

+0

很高兴我能帮忙:) – 2013-04-05 08:11:27

0

我在过去使用了以下条件“必需的”&“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; } 
}