本文介绍了如何在不显示的情况下ASP MVC的ValidationSummary错误时,通过现场已经显示领域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不希望的情况下,它只显示已显示领域相关的错误显示在ValidationSummary。然而,当出现自定义服务器端验证错误,如确实需要的ValidationSummary:

I would not like to display the ValidationSummary in case it only displays already displayed field related errors. However I do need ValidationSummary when custom server side validation error occurs like:

if (!UserManager.IsEmailConfirmed(user.Id))
{
    AuthenticationManager.SignOut();
    ModelState.AddModelError("", "You need to confirm your email.");
    return View(model);
}

推荐答案

使用<$c$c>@Html.ValidationSummary(excludePropertyErrors:真) 。

这超载,当 excludePropertyErrors 真正,隐藏属性的错误就像你的的电子邮件字段是不是一个有效的E-mail地址。是必需的密码字段。的从验证摘要。参见@Html.ValidationSummary(true) - 什么是真正的做

This overload, when excludePropertyErrors is true, hides property errors like your "The Email field is not a valid e-mail address." and "The Password field is required." from the validation summary. See also @Html.ValidationSummary(true) - What's the true do?.

它的的检测是否打印那些虽然 @ Html.ValidationMessageFor(),因此,如果您忘记了任何这些,你可以未能获得不告诉你为什么他们失败表单提交。

It doesn't detect whether you print those though @Html.ValidationMessageFor(), so if you forget any of those, you can get failing form submissions that don't tell you why they fail.

要手动添加的的-property验证错误,通话 ModelState.AddModelError(,自定义错误)(注意空字符串)所解释的和ASP.NET MVC Html.ValidationSummary(真)不显示模型错误。

To manually add non-property validation errors, call ModelState.AddModelError("", "Custom error") (note the empty string) as explained in Add error message to @Html.ValidationSummary and ASP.NET MVC Html.ValidationSummary(true) does not display model errors.

这篇关于如何在不显示的情况下ASP MVC的ValidationSummary错误时,通过现场已经显示领域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-24 16:06