我有下面的代碼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。
你能發佈相關的控制器操作嗎? – 2012-08-01 20:16:37
嘗試流利的驗證http://fluentvalidation.codeplex.com/。 Theres還爲MVC http://nuget.org/packages/FluentValidation.MVC3提供了nuget包。 – cubski 2012-08-01 20:19:46
你如何在視圖中渲染它? – Shyju 2012-08-01 20:23:29