嗯,不,我沒有遇到過這樣的問題。我剛剛測試了下面的代碼,並且它按預期完美工作。
型號:
public class UsersRegisterUserViewModel
{
[DisplayName("E-Mail Address")]
[Required(ErrorMessage = "E-Mail Address is required")]
[RegularExpression(@"^[A-Za-z0-9_\-\.][email protected](([A-Za-z0-9\-])+\.)+([A-Za-z\-])+$", ErrorMessage = "Invalid E-mail Address")]
public string RegUsername { get; set; }
[Required]
[Display(Name = "Password")]
[DataType(DataType.Password)]
public string Password { get; set; }
[Required]
[Display(Name = "Confirm Password")]
[Compare("Password", ErrorMessage = "Passwords must match")]
[DataType(DataType.Password)]
public string RegConfirmPassword { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new UsersRegisterUserViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(UsersRegisterUserViewModel model)
{
return View(model);
}
}
查看:
@model UsersRegisterUserViewModel
<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.RegUsername)
@Html.EditorFor(x => x.RegUsername)
@Html.ValidationMessageFor(x => x.RegUsername)
</div>
<div>
@Html.LabelFor(x => x.Password)
@Html.EditorFor(x => x.Password)
@Html.ValidationMessageFor(x => x.Password)
</div>
<div>
@Html.LabelFor(x => x.RegConfirmPassword)
@Html.EditorFor(x => x.RegConfirmPassword)
@Html.ValidationMessageFor(x => x.RegConfirmPassword)
</div>
<input type="submit" value="OK" />
}
所以,現在的問題是:如何是你的代碼比我有什麼不同?
是的 - 有同樣的問題 – sydneyos 2011-08-23 00:06:31