I display some text in the view:
....
<%: model.Content %>
....
my model.Content contains html tags, and I want to display them not as text, but as html. How to do it?
Thanks.
The HTML content (data) will be displayed using Html. Raw Helper method in ASP.Net MVC Razor. In this article I will explain with an example, how to display HTML content (data) from Model in View in ASP.Net MVC Razor.
Step 1: Right click on the "Controllers" folder and add "LoadHtml" controller. Copy and paste the following code. Step 2: Right click on the "Index" action method in the "LoadHtmlController" and add "Index" view.
The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor. Configuring Bundles. Please refer the following article for complete information on how to configure Bundles in ASP.Net MVC project. Using Bundles (ScriptBundle) in ASP.Net MVC Razor.
DisplayFor<TModel,TValue>(HtmlHelper<TModel>, Expression<Func<TModel,TValue>>, String, String, Object) Returns HTML markup for each property in the object that is represented by the specified expression, using the template, an HTML field ID, and additional view data.
As of MVC 3 you can use:
@Html.Raw(model.Content)
Use:
<%: MvcHtmlString.Create(model.Content) %>
or
<%= model.Content %>
Because <%:
does Html encoding, while <%=
doesn't.
MvcHtmlString.Create
creates a 'save' Html string, which<%:
takes and prints out as is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With