如果模型无效,带有 exceptPropertyErrors = true 的 ASP.NET MVC Validationsummary 将呈现为空
假设您有一个标准的 ValidationSummary:
<%: Html.ValidationSummary(excludePropertyErrors: true) %>
如果 ModelState 包含属性的模型错误,但不包含模型本身的模型错误,则 ValidationSummary 会呈现以下 HTML:
<div class="validation-summary-errors"><ul><li style="display:none"></li></ul></div>
显示为空列表,但由于列表周围有红色边框,因此仍然可见。这对我来说似乎是一个错误。我可以关闭 ValidationSummary 帮助器呈现空列表的功能吗?
Say you have a standard ValidationSummary:
<%: Html.ValidationSummary(excludePropertyErrors: true) %>
If the ModelState contains model errors for properties but not for the model itself the ValidationSummary renders the following HTML:
<div class="validation-summary-errors"><ul><li style="display:none"></li></ul></div>
Which is displayed as an empty list but is still visible because of the red border around the list. This seems to be a bug to me. Can I turn off that the ValidationSummary helper will ever render an empty list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与 usr 的答案类似,我用以下方法修复了这个问题:
这样我就不必实际分叉原始版本。
不过,同意,这很烦人。
Similar to usr's answer, I fixed this with the following:
This way I don't have to actually fork the original.
Agreed, though, this is very annoying.
当我通过查看源代码发现没有解决方案后,我通过 fork MVC 版本的代码并修改一行代码解决了问题。
After I found out that there is no solution by looking at the source I solved the problem by forking the MVC version of the code and modifying one line.
Bootstrap 类修复的另一个变体是:
Another variation of the fix with Bootstrap classes is:
另一种解决方法是将样式移至 div。 CSS 如下:
当摘要返回且没有错误时,div 不会显示。当出现错误时,该类会自动更改为validation-summary-errors。
Another workaround is to move the style to the div. Here's the CSS:
When the summary returns with no errors the div is not displayed. When there are errors the class is automatically changed to validation-summary-errors.