2012-08-01 75 views
2

我有下面的代碼MVC比較屬性不工作在服務器端

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

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

當我表現形式,在客戶端驗證工作,但是當我測試它在服務器端,它總是有效(試着用密碼= pass1234和ConfirmPassword = nonmatchingpassword)

我也嘗試了一些其他的屬性,如在http://foolproof.codeplex.com/等EqualTo但同樣的問題。

我已經包括方法

[HttpPost] 
[ValidateAntiForgeryToken] 
public ActionResult Register(RegisterViewModel model) 
{ 
    if (WebSecurity.IsAuthenticated) 
    { 
     return RedirectToAction("Index", "Home"); 
    } 

    if (ModelState.IsValid) 
    { 
     // register user... 
    } 

    model.Countries = this.Countries.FindAll().OrderBy(c => c.Name); 
    return View(model); 
} 

,這是我如何使它

@using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "form", @id = "register-form" })) 
{ 
@Html.AntiForgeryToken() 
<ul class="form-fields"> 
    ... 
    <li class="form-field"> 
     @Html.LabelFor(m => m.Password) 
     @Html.ValidationMessageFor(m => m.Password) 
     @Html.EditorFor(m => m.Password) 
    </li> 
    <li class="form-field"> 
     @Html.LabelFor(m => m.ConfirmPassword) 
     @Html.ValidationMessageFor(m => m.ConfirmPassword) 
     @Html.EditorFor(m => m.ConfirmPassword) 
    </li> 
    ...  
    <li class="form-actions"> 
     <button class="submit">register</button> 
    </li> 
</ul> 
} 

任何想法可能是錯誤的?我使用MVC4 RC。

+0

你能發佈相關的控制器操作嗎? – 2012-08-01 20:16:37

+0

嘗試流利的驗證http://fluentvalidation.codeplex.com/。 Theres還爲MVC http://nuget.org/packages/FluentValidation.MVC3提供了nuget包。 – cubski 2012-08-01 20:19:46

+0

你如何在視圖中渲染它? – Shyju 2012-08-01 20:23:29

回答

1

這是微軟如何做它的默認MVC項目模板:

[PropertiesMustMatch("Password", "ConfirmPassword", ErrorMessage = "The     password and confirmation password do not match.")] 
    public class RegisterModel 
    { 
     [Required] 
     [DisplayName("User name")] 
     public string UserName { get; set; } 

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

     [Required] 
     [ValidatePasswordLength] 
     [DataType(DataType.Password)] 
     [DisplayName("Password")] 
     public string Password { get; set; } 

     [Required] 
     [DataType(DataType.Password)] 
     [DisplayName("Confirm password")] 
     public string ConfirmPassword { get; set; } 
} 

我不知道你究竟是如何做你的服務器端測試。如果這不適合你,也許你可以發佈失敗的測試?

0

在第二項上取出StringLength。你不需要它,因爲第一個項目已經有了它。

+0

不,同樣的問題。 – FrEaKmAn 2012-08-01 20:23:36

+1

@FrEaKmAn - 你也不需要所需的。 – 2012-08-01 20:27:23

0

晚會有點晚,但我只是碰到類似的問題。這是由jquery unobstrusive JavaScript文件中的錯誤引起的。更高版本將修復它,我只是跑

Install-Package jQuery.Validation.Unobtrusive

裏面裝V2,這對我來說工作正常。你的旅費可能會改變。

此問題已經正確地回答here