Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display encoded HTML as decoded in MVC 3 Razor?

I'm using Razor in MVC 3 and Asp.net C#.

I have a View with the following code. model.ContentBody has some HTML tags.

I would need display this HTML content as DECODED.

How shall I change my code in the View?

 <div class="display-field">         @Html.DisplayFor(model => model.ContentBody)  </div> 
like image 264
GibboK Avatar asked Oct 05 '12 06:10

GibboK


People also ask

What symbol is used by the razor engine in MVC to automatically encode HTML output?

You add code to a page using the @ character When you display content in a page using the @ character, as in the preceding examples, ASP.NET HTML-encodes the output.

What is HTML encode and decode?

HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. For example, when embedded in a block of text, the characters < and > are encoded as &lt; and &gt; for HTTP transmission.

What is HTML encoding in MVC?

HtmlEncode is only meant to encode characters for display in HTML. It specifically does not encode whitespace characters. I would go with your first option, and make it an extension method for HtmlHelper.

Does MVC use razor?

Razor is one of the view engines supported in ASP.NET MVC. Razor allows you to write a mix of HTML and server-side code using C# or Visual Basic.


Video Answer


1 Answers

<div class="display-field">     @Html.Raw(Model.ContentBody) </div> 

This code solved the problem!

like image 96
GibboK Avatar answered Sep 28 '22 22:09

GibboK