在我的控制器中捕获到异常后,我想为电子邮件字段设置错误消息:
...
catch (EmailAlreadyExistsException emailAlreadyExistsException)
{
ModelState.AddModelError("Useraccount_Email", "This is an error message");
RegisterViewModel viewModel = new RegisterViewModel
{
Useraccount = useraccount,
InformationMessages = InformationMessageHelper.GetInformationMessages()
};
return PartialView(viewModel);
}
...
错误消息现在应该显示在我的视图中:
@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>
...
但在这种情况下没有显示任何内容。:( 我做错什么了?