How to use Razor as asp: Literal?

I have a simple model:

public class MyModel { public string Text{get;set;} } 

I have a view that displays the Text property of MyModel:

 <p>@Model.Text</p> 

How can I render html tags from text as tags? For example, I have the text " <b>Text</b> ". I want to get bold text inside the p tag as a result:

Text

But Razor displays the text as is:

 <b>Text</b> 
+8
tags razor
source share
1 answer

I think you need to use it like:

 <p>@Html.Raw(Model.Text)</p> 

Read more about here on the Phil Haack blog.

anurse points out in the comments that you could, alternatively, set the Text member type of your view model type to IHtmlString and just use @Model.Text for the output. ASP.NET MVC is smart enough to realize that output should not be escaped.

+19
source share

All Articles