CyMark
CyMark

Reputation: 95

Disable HTML rendering with MVC

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 "&quote" like this:

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

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

Answers (3)

Radim K&#246;hler
Radim K&#246;hler

Reputation: 123861

Razor engine by default always HtmlEncodes any string. If you need to override this use

@Html.Raw(result)

Upvotes: 3

Brettski
Brettski

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

CyMark
CyMark

Reputation: 95

Found the answer:

<tr @Html.Raw(result)  >

Also should be mod 2 and not 1.

Upvotes: 0

Related Questions