【发布时间】:2012-08-01 20:10:23
【问题描述】:
我有以下代码
[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。
【问题讨论】:
-
你能发布相关的控制器动作吗?
-
尝试流畅的验证fluentvalidation.codeplex.com。还有一个用于 MVC nuget.org/packages/FluentValidation.MVC3 的 nuget 包。
-
你是如何在视图中渲染它的?
-
“服务器端测试”到底是什么意思?您发布的模型是域对象还是视图模型?
-
它是视图模型和除比较之外的所有其他验证工作。
标签: c# asp.net-mvc-4