2011-12-12 22 views
4

在MVC3您可以添加验证模型,以检查是否属性匹配,就像这样:MVC3验证检查,如果属性值不同

public string NewPassword { get; set; } 

[Compare("NewPassword", 
ErrorMessage = "The new password and confirmation password do not match.")] 
public string ConfirmPassword { get; set; } 

有没有一种方法来检查两个属性在下面做,相信喜欢不同码?

[CheckPropertiesDiffer("OldPassword", 
ErrorMessage = "Old and new passwords cannot be the same")] 
public string OldPassword { get; set; } 

public string ConfirmPassword { get; set; } 

回答

4

这里是你可以在模型中使用什么:

public string OldPassword 

[NotEqualTo("OldPassword", ErrorMessage = "Old and new passwords cannot be the same.")] 
public string NewPassword { get; set; } 

然后定义如下荷兰国际集团自定义属性:

public class NotEqualToAttribute : ValidationAttribute 
{ 
    private const string defaultErrorMessage = "{0} cannot be the same as {1}."; 

    private string otherProperty; 

    public NotEqualToAttribute(string otherProperty) : base(defaultErrorMessage) 
    { 
     if (string.IsNullOrEmpty(otherProperty)) 
     { 
      throw new ArgumentNullException("otherProperty"); 
     } 

     this.otherProperty = otherProperty; 
    } 

    public override string FormatErrorMessage(string name) 
    { 
     return string.Format(ErrorMessageString, name, otherProperty); 
    } 

    protected override ValidationResult IsValid(object value, ValidationContext validationContext) 
    { 
     if (value != null) 
     { 
      PropertyInfo otherPropertyInfo = validationContext.ObjectInstance.GetType().GetProperty(otherProperty); 

      if (otherPropertyInfo == null) 
      { 
       return new ValidationResult(string.Format("Property '{0}' is undefined.", otherProperty)); 
      } 

      var otherPropertyValue = otherPropertyInfo.GetValue(validationContext.ObjectInstance, null); 

      if (otherPropertyValue != null && !string.IsNullOrEmpty(otherPropertyValue.ToString())) 
      { 
       if (value.Equals(otherPropertyValue)) 
       { 
        return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName)); 
       } 
      } 
     } 

     return ValidationResult.Success; 
    }   
} 
+0

+1 - 真棒,只是我在找什么,谢谢! – adrianos

+0

@危险我把这个添加到我的项目中,但是当两个字段都相同时,错误不会显示。你能帮我吗? –

+0

@Mohit - 可能有很多可能性,为什么它可能不适合你自己。我建议你在SO上发布这个新问题,详细说明你正在努力实现的目标以及迄今为止尝试过的内容。 – Dangerous

6

我会检查控制器。

在控制器:

if(model.ConfirmPassword == model.OldPassword){ 
    ModelState.AddModelError("ConfirmPassword", "Old and new passwords cannot be the same"); 
} 

在View:

@Html.ValidationMessage("ConfirmPassword") 

希望这有助于

+0

谢谢,我可能会尝试 – adrianos

+0

+1感谢这种方式运作良好,这里是最简单的解决方案 - 但我会与Dangerous一起回答,因为NotEqualTo ValidationAttribute解决方案可以在其他地方重复使用。 – adrianos

2

你也可以实现一流水平的验证就像这里的描述:http://weblogs.asp.net/scottgu/archive/2010/12/10/class-level-model-validation-with-ef-code-first-and-asp-net-mvc-3.aspx

基本上你实现IValidatableObject的验证方法,并且可以访问任何你想要的属性。

public class MyClass : IValidateableObject 
{ 
    public string NewPassword { get; set; } 
    public string OldPassword { get; set; } 

    public IEnumerable<ValidationResult> Validate(ValidationContext context) 
    { 
     if (NewPassword == OldPassword) 
      yield return new ValidationResult("Passwords should not be the same"); 
    } 
} 
+0

+1非常酷,谢谢 – adrianos