2012-04-15 61 views
1

我使用以下模型來控制用戶輸入並註冊他。 但是,如果我輸入相同的密碼並按下按鈕,那麼顯示有關該確認密碼的消息是錯誤的。MVC3中的確認密碼問題

我確信模型是正確的,因爲它在其他頁面中工作。 在這種情況下RegisterModelSecondEventFrontEndViewModel的成員。

所以當我評論[Compare("RegisterModel.Password", ErrorMessage = "The password and confirmation password do not match.")]它的作品,但沒有確認我需要!

任何線索如何解決它?

public class EventFrontEndViewModel 
    { 
     public Page CurrentPage { set; get; } 

     public List<Event> Events { set; get; } 

     public List<Event> SubscribedEvents { set; get; } 

     public RegisterModelSecond RegisterModel { set; get; } 

     public EventFrontEndViewModel() 
     { 
      CurrentPage = new Page(); 
      Events = new List<Event>(); 
      RegisterModel = new RegisterModelSecond(); 
      SubscribedEvents = new List<Event>(); 
     } 
    } 

註冊型號

public class RegisterModelSecond 
    { 
     [Required] 
     [Display(Name = "User name")] 
     public string UserName { get; set; } 

     [Required] 
     [DataType(DataType.EmailAddress)] 
     [Display(Name = "Email address")] 
     public string Email { get; set; } 

     [Required] 
     [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
     [DataType(DataType.Password)] 
     [Display(Name = "Password")] 
     public string Password { get; set; } 

     [Required] 
     [DataType(DataType.Password)] 
     [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)] 
     [Display(Name = "Confirm password")] 
     [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
     public string ConfirmPassword { get; set; } 
    } 

和HTML

<div class="editor-label"> 
             @Html.LabelFor(m => m.RegisterModel.Password) 
            </div> 
            <div class="editor-field"> 
             @Html.PasswordFor(m => m.RegisterModel.Password) 
             @Html.ValidationMessageFor(m => m.RegisterModel.Password) 
            </div> 
            <div class="clear"> 
            </div> 
            <div class="editor-label"> 
             @Html.LabelFor(m => m.RegisterModel.ConfirmPassword) 
            </div> 
            <div class="editor-field"> 
             @Html.PasswordFor(m => m.RegisterModel.ConfirmPassword) 
             @Html.ValidationMessageFor(m => m.RegisterModel.ConfirmPassword) 
            </div> 
            <div class="clear"> 
            </div> 

回答

2

我發現這裏MVC 3 Client-Side Compare Validation

正確答案這是在客戶端驗證腳本錯誤:jquery.validate。 unobtrusive.js

在行〜284,你會發現這一點:

element = $(options.form).find(":input[name=" + fullOtherName + "]")[0]; 

它改成這樣:

element = $(options.form).find(":input[name='" + fullOtherName + "']")[0]; 

name屬性需要單引號。


我想添加如果你使用MIN版本,你必須在那裏改變它。

+1

謝謝男人...完全瘋狂的bug ... – kape123 2012-04-22 05:00:11

+1

哇..你救我一天..謝謝 – 2013-04-16 11:44:03