我的 ModelState.AddModelError 没有显示错误。我检查了用户名并在重复时显示错误。当我使用断点时,我看到 ModelState.AddModelError 已填充,它只是不显示错误消息。
我的代码:
控制器:
[HttpPost]
public ActionResult Register(tbl_User model)
{
if (ModelState.IsValid)
{
if (MyClass.IsUserNameDuplicate(model.username) == true)
{
this.ModelState.AddModelError("username", "the username is duplicate");
return View(model);
}
else
{
MyClass.creatUser(model.username, model.password_User, model.nam);
}
}
return View(model);
}
看法 :
@Html.ValidationSummary(true)
@Html.ValidationMessageFor(model => model.username)
@Html.TextBoxFor(m => m.username, new {@class = "input", @placeholder = "*enter user name" })