2015-10-08 29 views
0

我正在使用Asp.net MVC創建密碼並確認密碼字段。我正在使用遠程屬性來檢查密碼和確認密碼是否相同,但是如果遠程將應用它的方框更改,則只會調用一個函數。設置密碼/確認密碼沒有[比較]

我查看了以前的帖子回去年,發現最常見的建議是比較屬性,但它現在已被棄用。

我假設這個問題有一個預先構建的解決方案。

這是從模型

[Remote(UserController.ActionNameConstants.PasswordMatch, 
     UserController.NameConst, AdditionalFields = "ConfirmPassword", 
     ErrorMessage = "The passwords do not match")] 
    [MinLength(8, ErrorMessage="Minimum password length is 8")] 
    [DisplayName("Password"), RequiredIf("Id == 0", 
     ErrorMessage="Password is required")] 
    public string Password { get; set; } 


    [DisplayName("Confirm Password"), RequiredIf("Id == 0", 
     ErrorMessage = "Confirm password is required")] 
    public string ConfirmPassword { get; set; } 

這是在控制器

[HttpGet] 
    public virtual JsonResult PasswordMatch(string password,string confirmPassword) 
    { 
     return this.Json(password == 
     confirmPassword,JsonRequestBehavior.AllowGet); 
     } 
+0

因此,經過進一步的研究,看起來仍然存在不被棄用的比較版本,所以使用Compare的任何解決方案仍然使用System.ComponentModel.DataAnnotation.Compare()有效。 –

回答

1

比較不貶值了,你仍然可以使用[比較(「屬性名與比較」)..它位於「System.ComponentModel.DataAnnotations」命名空間中。