2012-12-08 85 views
2

您好我是MVC的新手,我正在嘗試製作一張顯示替代顏色行的表格。事情是這樣的:停用MVC的HTML渲染

@foreach (var item in Model) { 

var result = ""; 
var styleWhite = @"style=""background-color:white"""; 
var styleBlue = @"style=""background-color:blue"""; 

if ((item.ID % 1) == 0) 
{ result = styleBlue; } 
else 
{ result = styleWhite; } 

<tr @result > 
    <td> 
     @Html.DisplayFor(modelItem => item.CALLTYPE) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.DESCRIPTION) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | 
     @Html.ActionLink("Details", "Details", new { id=item.ID }) | 
     @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 
    </td> 
</tr> 
} 

但是雙引號一直呈現爲「&報價」是這樣的:

<tr style=&quot;background-color:blue&quot; > 

有沒有辦法來抑制渲染,並獲得雙引號(或單引號)在標籤裏面? 感謝您的幫助。

回答

3

默認情況下Razor引擎總是HtmlEncodes任何字符串。如果你需要重寫此使用

@Html.Raw(result) 
0

找到了答案:

<tr @Html.Raw(result) > 

也應該是MOD 2,而不是1

0

使用單引號代替:

@foreach (var item in Model) { 

var result = string.Empty; 
var styleWhite = @"style='background-color:white'"; 
var styleBlue = @"style='background-color:blue'"; 

if ((item.ID % 1) == 0) 
{ result = styleBlue; } 
else 
{ result = styleWhite; } 

<tr @result > 
    <td> 
     @Html.DisplayFor(modelItem => item.CALLTYPE) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.DESCRIPTION) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | 
     @Html.ActionLink("Details", "Details", new { id=item.ID }) | 
     @Html.ActionLink("Delete", "Delete", new { id=item.ID }) 
    </td> 
</tr> 
} 
+1

感謝您的回覆,但它呈現爲「&39」。似乎按照下面的@ Html.Raw似乎工作。 – CyMark