Reputation: 95
Hi I am new to MVC and I am trying to make a table that shows alternate colour rows. Something like this:
@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>
}
However the double quote keeps rendering as ""e" like this:
<tr style="background-color:blue" >
Is there a way to suppress the rendering and get the double quote (or single quote) inside the tag? Thanks for any assistance.
Upvotes: 2
Views: 644
Reputation: 123861
Razor engine by default always HtmlEncodes any string. If you need to override this use
@Html.Raw(result)
Upvotes: 3
Reputation: 20081
Use a single quote instead:
@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>
}
Upvotes: 0
Reputation: 95
Found the answer:
<tr @Html.Raw(result) >
Also should be mod 2 and not 1.
Upvotes: 0