gpt4 book ai didi

.net - 从MVC3升级到MVC4后“Element not closed”错误

转载 作者:行者123 更新时间:2023-12-04 13:23:58 26 4
gpt4 key购买 nike

Razor 2(与MVC4一起提供)似乎与Razor 1(来自MVC3)没有完全向后兼容。

自升级以来,我发现了一个错误:

The "td@Html.Raw(count" element was not closed. All elements must be either self-closing or have a matching end tag.



导致此的代码是:
<td@Html.Raw(count == null ? null : " class='has-item'")>

有什么解决方案?

最佳答案

Razor解析器已针对MVC 4 presumably because进行了重写:

The HTML5-specs clearly states that unclosed HTML-tags are supported, but Razor v1 didn't have an advanced enough parser to support this. Razor v2 now supports this with the elements listed in W3C's spec.



这里最简单的解决方案是在 @符号之前添加一个空格:
<td @Html.Raw(count == null ? null : " class='has-item'")>

但是,带有MVC 4的Razor中的条件属性具有更优雅的语法。
<td class="@(count == null ? null : "has-item")">

当属性值解析为 null时,该属性将从元素中省略。因此,此标记的输出为:
<td>

...或者...
<td class="has-item">

关于.net - 从MVC3升级到MVC4后“Element not closed”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12694814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com