的ValidationSummary局部视图中没有显示错误 [英] ValidationSummary inside a partial view not showing errors
问题描述
我有一个这样的局部视图(简体):
I have a partial view like this (simplified):
@model Portal.Models.LoginModel
<div class="login-container k-block">
<section id="login-form" class="">
@using (Html.BeginForm(actionName, controllerName, new { ReturnUrl = ViewBag.ReturnUrl }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset id="login-form-list-items">
<ol>
<li>
@Html.LabelFor(m => m.CardNumber)
@Html.TextBoxFor(m => m.CardNumber, new { @class="k-textbox"})
<div class="k-error-colored">
@Html.ValidationMessageFor(m => m.CardNumber)
</div>
</li>
<li>
@Html.LabelFor(m => m.Pin)
@Html.PasswordFor(m => m.Pin, new { @class="k-textbox"})
<div class="k-error-colored">
@Html.ValidationMessageFor(m => m.Pin)
</div>
</li>
<input id="login-input-submit" class="k-button" type="submit" value="Enter" />
</fieldset>
</div>
在我的登录
查看我叫这样的局部视图:
And in my login
view I call this partial view like:
@model Portal.Models.LoginModel
@Html.Partial("_LoginFormNoRegistration", Model, new ViewDataDictionary { { "actionName", "Login" }, { "controllerName", "Account" } })
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
问题是,当在控制器中的登录
方法添加了一个错误,如:
The problem is that when the login
method in the controller adds an error like:
public ActionResult Login(LoginModel model, string returnUrl)
{
//...
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
该消息不会在验证摘要显示...我不明白为什么......可能是什么问题呢?一些JavaScript库丢失?
The message is not show in the validation summary... I don't understand why... What could be the problem? Some javascript library missing?
更新
我还发现,所产生的形式 NOVALIDATE
属性设置:
I also found that the form generated as the novalidate
attribute set:
<form action="/" method="post" novalidate="novalidate">
//...
</form>
我不知道为什么。
I don't know why.
推荐答案
我发现这个问题。
我是通过一个新的ViewData在其中覆盖父视图的ViewData的的的RenderPartial,所以模型的状态丢失,如下解释:的传递额外的ViewData到ASP.NET MVC 4局部视图,而传播的ModelState错误。
I was passing a new ViewData in the RenderPartial which was overriding the ViewData of the parent view, so the model state was lost, as explained here: Pass Additional ViewData to an ASP.NET MVC 4 Partial View While Propagating ModelState Errors.
更改主视图:
@model Portal.Models.LoginModel
@{
ViewData.Add("actionName", "Login");
ViewData.Add("controllerName", "Account");
Html.RenderPartial("_LoginFormNoRegistration", Model, ViewData);
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
的伎俩!
另外,如果你想显示一般错误消息在模型中的的ValidationSummary
时,一定要带一个空字符串作为键添加错误:
Also, if you want to show a general error message for the model in the validationsummary
, be sure to add the error with an empty string as key:
ModelState.AddModelError(错误,提供的用户名或密码不正确。);
- 不起作用
ModelState.AddModelError(,提供的用户名或密码不正确);
- 工作
这篇关于的ValidationSummary局部视图中没有显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!