我有以下代码
[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; }
当我显示表单时,客户端验证有效,但是当我在服务器端测试它时,它始终有效(尝试使用 Password=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。