ModelState.AddModelError - 显示任何错误 [英] ModelState.AddModelError - no error shown
本文介绍了ModelState.AddModelError - 显示任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想设置一个错误消息的电子邮件字段中的异常已经在我的控制器被抓后:
I'd like to set an error message for an email field after an exception has been caught in my controller:
...
catch (EmailAlreadyExistsException emailAlreadyExistsException)
{
ModelState.AddModelError("Useraccount_Email", "This is an error message");
RegisterViewModel viewModel = new RegisterViewModel
{
Useraccount = useraccount,
InformationMessages = InformationMessageHelper.GetInformationMessages()
};
return PartialView(viewModel);
}
...
现在应显示在我看来,错误消息:
The error message should now be displayed in my view:
@using (Ajax.BeginForm("Register", new AjaxOptions { HttpMethod = "Post", OnSuccess = "registerAjaxSuccess" }))
{
@Html.ValidationSummary(true)
<div class="ym-grid">
<div class="ym-g33 ym-gl">
<div class="ym-gbox">
@Html.LabelFor(model => model.Useraccount.LastName)
</div>
...
<div class="ym-grid">
<div class="ym-g33 ym-gl">
<div class="ym-gbox">
@Html.LabelFor(model => model.Useraccount.Email)
</div>
</div>
<div class="ym-g33 ym-gl">
<div class="ym-gbox">
@Html.EditorFor(model => model.Useraccount.Email, new { required = "required" })
</div>
</div>
<div class="ym-g33 ym-gr">
<div class="ym-gbox">
@Html.ValidationMessageFor(model => model.Useraccount.Email)
</div>
</div>
</div>
...
但是,没有什么是在这种情况下示出。 :(
我做了什么错了?
But nothing is shown in this case. :( What did I do wrong?
推荐答案
在增加模型误差的正确分离复杂对象时是点。
,而不是unserscore _
When adding model errors the correct separator when working with complex objects is the dot .
and not the unserscore _
所以下面的调用应该工作:
So the following call should work:
ModelState.AddModelError("Useraccount.Email", "This is an error message");
这篇关于ModelState.AddModelError - 显示任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文