2

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.

3 Answers 3

3

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

@Html.Raw(result)
Sign up to request clarification or add additional context in comments.

Comments

0

Found the answer:

<tr @Html.Raw(result)  >

Also should be mod 2 and not 1.

Comments

0

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>
}

1 Comment

Thanks for the reply, but it renders to "&39". Seems that @Html.Raw as per below seems to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.