我有下面的代码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