WinFXGuy
WinFXGuy

Reputation: 1599

Razor - How to display html content in a div tag?

Here is my razor code:

       <fieldset>
            <legend>Headline News</legend>

                @foreach (var item in Model)
                {
                    <div>@Html.DisplayFor(modelitem => item.Description)</div>
                }
        </fieldset>

The value for item.Description is HTML:

<table border="0" cellpadding="2" cellspacing="7" style="vertical-align:top;"><tr><td width="80" align="center" valign="top">
   {{ table info }}
</table>

I actually want to display the HTML content, but it shows up as HTML tags. Thanks in advance!

Upvotes: 5

Views: 21923

Answers (3)

Mateusz Rogulski
Mateusz Rogulski

Reputation: 7445

You should use:

@foreach (var item in Model)
{
    <div>@Html.Raw(item.Description)</div>
}

Upvotes: 19

pollirrata
pollirrata

Reputation: 5286

You don't necessarily need to use @Html.Raw, unless there are some HTML tags that need to be parsed as HTML. You can also use

<fieldset>
<legend>Headline News</legend>
@foreach (var item in Model)
{
<div>@item.Description</div>
}
</fieldset>

If you want to use @Html.Raw, just be sure on using @Html.AttributeEncode to prevent scripting injection

Upvotes: 0

David East
David East

Reputation: 32604

You need to use @Html.Raw()

@Html.DisplayFor(modelitem => @Html.Raw(item.Description))

Upvotes: 2

Related Questions