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

19,751

Solution 1

You should use:

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

Solution 2

You need to use @Html.Raw()

@Html.DisplayFor(modelitem => @Html.Raw(item.Description))
Share:
19,751
WinFXGuy
Author by

WinFXGuy

Updated on June 04, 2022

Comments

  • WinFXGuy
    WinFXGuy almost 2 years

    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!

  • WinFXGuy
    WinFXGuy almost 12 years
    However, the correct syntax that worked for me is: <div>@Html.Raw(item.Description) </div>
  • WinFXGuy
    WinFXGuy almost 12 years
    How to use @Html.AttributeEncode ? Also, <div>@item.Description</div> gives me syntax error.