ModelState.AddModelError不显示错误 [英] ModelState.AddModelError dosen't show error
本文介绍了ModelState.AddModelError不显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我项目中的
ModelState.AddModelError不显示错误。当然得到错误但无法显示。
我的控制器:
hi
in my project ModelState.AddModelError dosen't show error . of course get error but can't show.
my controller:
[HttpPost]
public ActionResult Register(tbl_User model)
{
if (ModelState.IsValid)
{
if (MyClass.IsUserNameDuplicate(model.username) == true)
{
this.ModelState.AddModelError("username", "user name is duplicate");
return View(model);
}
else
{
MyClass.creatUser(model.username, model.password_User, model.nam);
}
}
return View(model);
}
我的观点:
my view :
@Html.ValidationSummary(true)
@Html.ValidationMessageFor(model => model.username)
@Html.TextBoxFor(m => m.username, new {@class = "input", @placeholder = "*enter your username" })
推荐答案
您不需要指定密钥。正确如下:
You need to don't specify the key. Correct like this:
ModelState.AddModelError("", "user name is duplicate");
像这样的视图
Like this the view
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
显示错误
display error
这篇关于ModelState.AddModelError不显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文