2012-03-24 104 views
0

不知道這是可以預料的,但它是一個讓我吃驚..MVC驗證工作不正常

我強烈地打字與RegistrationViewModel一個觀點,但是當我試圖提交表單,驗證在2個地方錯誤地驗證。

  1. 當我輸入「測試」時,電子郵件字段不會拋出驗證錯誤。
  2. 密碼驗證程序說,密碼不匹配時,他們這樣做。

任何人都知道爲什麼會發生這種情況?

ViewModel類:

public class RegistrationViewModel 
{ 
    public RegisterModel RegistrationData { get; set; } 
    ...  
} 

驗證屬性對於RegistrationData類的屬性:

public class RegisterModel 
{ 
    [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; } 

    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The Password and Confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

    [Required] 
    [Display(Name = "First name")] 
    [StringLength(20, ErrorMessage = "First name must be between 2 and 20 characters.", MinimumLength = 2)] 
    public string FirstName { get; set; } 

    [Required] 
    [Display(Name = "Last name")] 
    [StringLength(20, ErrorMessage = "Last name must be between 2 and 20 characters.", MinimumLength = 2)] 
    public string LastName { get; set; } 
} 

查看:

@model PropertyManager_MVC.Areas.Account.ViewModels.RegistrationViewModel 
<div class="editor-field"> 
      @Html.TextBoxFor(m => m.RegistrationData.Email) 
      @Html.ValidationMessageFor(m => m.RegistrationData.Email) 
     </div> 
<div class="editor-label"> 
      @Html.LabelFor(m => m.RegistrationData.Password) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(m => m.RegistrationData.Password) 
      @Html.ValidationMessageFor(m => m.RegistrationData.Password) 
     </div> 
     <div class="editor-label"> 
      @Html.LabelFor(m => m.RegistrationData.ConfirmPassword) 
     </div> 
     <div class="editor-field"> 
      @Html.PasswordFor(m => m.RegistrationData.ConfirmPassword) 
      @Html.ValidationMessageFor(m => m.RegistrationData.ConfirmPassword) 
     </div> 
+0

你指的是服務器端驗證? – Jon 2012-03-24 16:30:27

+0

不,客戶端驗證 – Greg 2012-03-24 16:32:34

+0

客戶端驗證和不顯眼的js都在webconfig中啓用。 – Greg 2012-03-24 16:33:37

回答

2

添加該正則表達式email屬性和它會照顧它。

[Required] 
    [DataType(DataType.EmailAddress)] 
    [Display(Name = "Email address")] 
    [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9]+(\\.[a-z]{2,4})$", ErrorMessage = "Not a valid email")] 
    public string Email { get; set; } 

在確認密碼字段前添加必需的屬性。

[Required] 
    [DataType(DataType.Password)] 
    [Display(Name = "Confirm password")] 
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] 
    public string ConfirmPassword { get; set; } 

Scottgu的例子來處理這個問題。 http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx

+0

謝謝你的工作 - 那麼DataType.Email做什麼呢? – Greg 2012-03-24 16:42:08

+1

@Greg - DataType屬性不影響驗證,而是顯示和編輯器模板。當使用Html.DisplayFor時,DataType.Password會使DisplayTemplate將密碼格式化爲mailto:鏈接。如果你願意,你也可以重寫這個並創建你自己的Email模板。 – 2012-03-24 19:51:19

+0

雖然我第一次測試時工作正常,但驗證錯誤已恢復(即使更改仍在原位)。對不起Shyju,我已經標記了您的答案。我需要爲此找到解決方案。 – Greg 2012-03-25 10:40:05