"元素没有闭合QUOT;从升级到MVC3后MVC4错误 [英] "Element not closed" error after upgrading from MVC3 to MVC4

查看:209
本文介绍了"元素没有闭合QUOT;从升级到MVC3后MVC4错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

剃刀2(其中附带MVC4)似乎并没有与剃刀1(从MVC3)完全向后兼容。

Razor 2 (which ships with MVC4) doesn't seem to be fully backwards compatible with Razor 1 (from MVC3).

由于升级换代,我发现了一个错误:

Since upgrading, I found an error:

在td@Html.Raw(伯爵元素没有被封闭,所有的元素必须是自闭或有一个匹配的结束标记。

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

这引起了code,这是:

The code that caused this was:

<td@Html.Raw(count == null ? null : " class='has-item'")>

该解决方案是什么呢?

What is the solution to this?

推荐答案

剃刀分析程序重新编写的MVC 4的$p$psumably因为

The Razor parser was re-written for MVC 4, presumably because:

在HTML5的规范中明确指出,未关闭的HTML标记来支持,但剃刀V1没有一个足够先进解析器来支持这一点。剃刀V2现在支持这个在W3C的规范中列出的元素。

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.

这里最简单的解决方案是将 @ 符号前增加一个空格:

The simplest solution here is to add a single space before the @ symbol:

<td @Html.Raw(count == null ? null : " class='has-item'")>

然而,在剃刀与MVC 4条件属性有一个更优雅的语法。

However, conditional attributes in Razor with MVC 4 have a more elegant syntax.

<td class="@(count == null ? null : "has-item")">

当属性值解析为,该属性从元件省略。所以这个标记的输出可以是:

When an attribute value resolves to null, the attribute is omitted from the element. So the output of this markup is either:

<td>

...或...

...or...

<td class="has-item">

这篇关于&QUOT;元素没有闭合QUOT;从升级到MVC3后MVC4错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆