0

I am working on an ASP.NET MVC 5 application and using tinymce editor to store HTML formatted data (for certain fields) to a SQL Server 2014 database.

The data is being returned to the page using a foreach loop, but the tags/markup are not being rendered as HTML and instead are showing up in the page as text.

CODE EX:

@foreach (var item in Model) {
    <div>@Html.DisplayFor(modelItem => item.Category)</div>
    <div>@Html.DisplayFor(modelItem => item.Name)</div>
    <div>@Html.DisplayFor(modelItem => item.Description)</div>
}

How do I get the stored HTML to render properly when returned to the page?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jason
  • 3
  • 3
  • 1
    Use `@Html.Raw()` if your values contain html tags –  Aug 19 '17 at 12:47
  • check this https://stackoverflow.com/a/7777718/6448640 –  Aug 19 '17 at 12:51
  • When I replace the Html.Display with HTML.Raw I get the following error: Cannot convert lambda expression to type 'string' because it is not a delegate type – jason Aug 19 '17 at 12:53
  • I have replaced the call with the following, but since I am a bit new to Razor, I do not think I have implemented it correctly because the markup is still being rendered as text : @Html.Raw(Html.Encode(item.Description)) – jason Aug 19 '17 at 13:06

2 Answers2

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

You need to use Html.Raw as this render html content into page.

D Mishra
  • 1,518
  • 1
  • 13
  • 17
0

Use @Html.Raw()

Cannot convert lambda expression to type 'string' because it is not a delegate type

This error can be avoid by using Raw(@item.Category). Try:

@foreach (var item in Model) {    
    <div>@Html.Raw(@item.Category)</div>
    <div>@Html.Raw(@item.Name)</div>
    <div>@Html.Raw(@item.Description)</div>
}
  • A word of caution. `Html.Raw()` introduces a risk of cross site scripting. You should use it only with trusted data. – ste-fu Aug 19 '17 at 14:08