Jen
Jen

Reputation: 2004

How to handle Html tags in string - when displayed in MVC page

So I'm retrieving data from the Rally web service and the description field contains html tags.

My MVC page looks like this:

<table width="100%" id="stories">
        <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Description</td>
            <td>TaskEstimateTotal</td>
        </tr>
        @foreach (var story in Model.UserStories)
        {
            <tr>
                <td>@story["FormattedID"]</td>
                <td>@story["Name"]</td>
                <td>@story["Description"]</td>
                <td>@story["TaskEstimateTotal"]</td>
            </tr>
        }
    </table>

The html tags appear as text eg:

<DIV>As a Marketing Analytics And Data Manager</DIV>

I've tried encoding and decoding it but neither of those are producing the desired response. Encoding it will convert the < into &lt; type text.

<td>@HttpUtility.HtmlEncode(story["Description"])</td>

Hopefully it's just something simple I've missed!

Unless I should be stripping out the tags?

Upvotes: 0

Views: 1666

Answers (2)

Sidharth Mudgal
Sidharth Mudgal

Reputation: 4264

You can do:

<td>@Html.Raw(story["Description"])</td>

Razor html encodes strings by default.

Upvotes: 1

Mightymuke
Mightymuke

Reputation: 5144

Have you tried:

<td>@Html.Raw(story["Description"])</td>

MSDN: http://msdn.microsoft.com/en-us/library/gg480740(v=vs.98).aspx

Upvotes: 3

Related Questions