15

How can I return a model with a string propertie containing <li> elements and display it in view? If I just write @Model.Messages it shows all the string.. i need it in html format.

AStopher
  • 4,207
  • 11
  • 50
  • 75
MuriloKunze
  • 15,195
  • 17
  • 54
  • 82

3 Answers3

31

You can use the Content method with the Content-Type text/html to return the HTML directly, without the need of Html.Raw.

public ActionResult MyHtmlView() {
    return Content("<html><body>Ahoy.</body></html>", "text/html")
}

You can pass whatever Content-Type you want, such text/xml.

Kim Tranjan
  • 4,521
  • 3
  • 39
  • 38
7

You don't say which rendering engine you're using:

MVC3:
@Html.Raw(Model.Description)

web_bod
  • 5,728
  • 1
  • 17
  • 25
7

Use Server.HtmlEncode() to send html to view and then use the Server.HtmlDecode() to get the html to display on the view.

Then you can use @Html.Raw(Server.HtmlDecode(str)).

Try this:

<div class='content'>     
   @Html.Raw(HttpUtility.HtmlDecode(Model.Message)); 
</div> 

Ref: Display encoded html with razor

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75