2012-12-08 86 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