2012-10-02 170 views

回答

14

爲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'")> 

然而,條件屬性在Razor中使用MVC 4有更優雅的語法。

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

當屬性值解析到null,屬性從元件省略。所以這個標記的輸出可以是:

<td> 

......或者......

<td class="has-item"> 
1

MVC4的剃刀解析器是MVC3不同。 Razor v3具有高級解析器功能,另一方面嚴格分析與MVC3相比。

如果您沒有以正確的方式使用剃鬚刀語法,則可能會在將MVC3轉換爲MVC4時遇到語法錯誤。

的一些常見的剃刀代碼錯誤未在剃刀不允許V2是解決方案:

- >避免使用中的意見服務器塊,除非有變量聲明部分。

Don’t : @{if(check){body}} 
Recommended : @if(check){body} 

- >當您已經在服務器範圍內時避免使用@。

Don’t : @if(@variable) 
Recommended : @if(variable) 

Don't : @{int a = @Model.Property } 
Recommended : @{int a = Model.Property } 
相關問題