不知道這是可以預料的,但它是一個讓我吃驚..MVC驗證工作不正常
我強烈地打字與RegistrationViewModel一個觀點,但是當我試圖提交表單,驗證在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>
你指的是服務器端驗證? – Jon 2012-03-24 16:30:27
不,客戶端驗證 – Greg 2012-03-24 16:32:34
客戶端驗證和不顯眼的js都在webconfig中啓用。 – Greg 2012-03-24 16:33:37