【发布时间】:2014-06-21 11:25:42
【问题描述】:
我正在尝试同时使用 ModelState.AddErrrorModel 和 Html.ValidationMessage("Account") 来显示错误消息。
但是,即使我可以调试在我的 POST 中添加的模型错误,当再次加载视图时,我也无法在视图中看到它。
代码在表单的 POST 上运行,我可以看到 ModelState.AddModelError 正在被调用(ex.Message 也有一个值):
try {
// code that fails in this case
}
catch (Exception ex)
{
logger.Error(ex);
ModelState.AddModelError("Register",ex.Message);
}
return RedirectToAction("Account", accountViewModel);
我的观点是这样的:
<h3>Get your free account now</h3>
@using (Html.BeginForm("Register", "Home", FormMethod.Post, new { @class = "form" }))
{
// trying to test different methods to get my errors... NOTHING works :)
@Html.ValidationSummary()
@Html.ValidationMessage("Register")
@Html.BusinessErrorMessageBox()
<div class="form-group">
<label>Email address</label>
@Html.TextBoxFor(m => m.RegisterViewModel.Email, new { @class = "form-control", placeholder = "Email", @type = "email" })
@Html.ValidationMessageFor(m => m.RegisterViewModel.Email)
</div>
<div class="form-group">
<label>Password</label>
@Html.PasswordFor(m => m.RegisterViewModel.Password, new { @class = "form-control", placeholder = "Password" })
@Html.ValidationMessageFor(m => m.RegisterViewModel.Password)
</div>
<div class="form-group">
<label>Your country (the country where you live or where your organization is registered in)</label>
@Html.DropDownListFor(model => model.RegisterViewModel.SelectedCountry, Model.RegisterViewModel.Countries, new { @class = "form-control" })
</div>
<input type="submit" class="btn btn-primary btn-lg" value="Get your free account now" />
}
出于实际原因,我的视图称为Account,而接受POST的方法称为“Register”。
但无论我做什么,ValidationMessage 或 ValidationSummary 中都没有显示错误。
知道我做错了什么吗?
编辑:
仅供参考:视图中有两种形式,如果这可能会产生影响的话。
编辑 2:
单击转到我的服务器的按钮时,我可以看到正在输出以下 HTML。值ex.Message,无处显示:
<div class="validation-summary-valid" data-valmsg-summary="true"><ul><li style="display:none"></li>
</ul></div><span class="field-validation-valid" data-valmsg-for="Business" data-valmsg-replace="true"></span><span class="field-validation-valid" data-valmsg-for="Register" data-valmsg-replace="true"></span>
【问题讨论】:
-
你试过
ModelState.AddModelError(string.Empty ,ex.Message);和@Html.ValidationSummary(true) -
刚试过。不幸的是,这没有帮助。仅供参考 - 页面上还有 2 个表格
标签: asp.net asp.net-mvc validation asp.net-mvc-4 view