Display string as html in asp.net mvc view

Mukesh Sharma picture Mukesh Sharma · Nov 14, 2013 · Viewed 194.9k times · Source

I have a controller which generate string containing html markups.Now when I am displaying it on views, it is displayed as simple string containing all tags. I tried to use Html helper to encode/decode to display it properly, but it is not working.

string str= "<a href="/Home/Profile/seeker">seeker</a> has applied to <a href="/Jobs/Details/9">Job</a> floated by you.</br>";

On my views,

@Html.Encode(str)

Answer

Jared picture Jared · Nov 14, 2013

You are close you want to use @Html.Raw(str)

@Html.Encode takes strings and ensures that all the special characters are handled properly. These include characters like spaces.