How to display encoded HTML as decoded in MVC 3 Razor?
Solution 1
<div class="display-field">
@Html.Raw(Model.ContentBody)
</div>
This code solved the problem!
Solution 2
@Html.Raw
was not work for me here is example:-
string name = "<p><span style="font-size: small; color: #ff0000;"><span style="font-size: small;">&nbsp;<span style="font-size: large; color: #000000;">Hi</span><br />&nbsp; <br />This is just a sample,<br />This will not work with @Html.Raw(),<br />";
<span>@Html.Raw(name);</span>
But this worked instead:-
@MvcHtmlString.Create(HttpUtility.HtmlDecode(@model.ContentBody))
or you can also use :-
@Html.Raw(HttpUtility.HtmlDecode(@model.ContentBody));
Solution 3
You could also decode html in the controller before sending the model to the view,
WebUtility.HtmlDecode()
public ActionResult Index(int id)
{
var content = _contentRepository.GetContent(id);
var classViewModel = new ClassViewModel
{
ContentBody = WebUtility.HtmlDecode(ClassViewModel.ContentBody)
};
return View(classViewModel);
}
Solution 4
Well, to summarize.. In my service/Controller, while returning the model to the view
....
Description = WebUtility.HtmlDecode(narration.Narration1)
My cshtml, where the tinyMCE is displayed.. just bind regularly (I was using HtmlAttributeHelper for other purpose. You can ignore it)
<div>
@Html.TextAreaFor(model => model.Description, HtmlAttributeHelper.ConditionalDisable(false, new {@class = "tinymce"}))
</div>
And in the cshtml page where the data is displayed..
......
<td class="col-word-wrap">@Html.Raw(HttpUtility.HtmlDecode(@item.Narration1))</td>
Solution 5
Please use to display Html use with decode.
@MvcHtmlString.Create(@Model.OurVision)
GibboK
A professional and enthusiastic Senior Front End Developer. Listed as top 2 users by reputation in Czech Republic on Stack Overflow. Latest open source projects Animatelo - Porting to JavaScript Web Animations API of Animate.css (430+ stars on GitHub) Industrial UI - Simple, modular UI Components for Shop Floor Applications Frontend Boilerplate - An opinionated boilerplate which helps you build fast, robust, and adaptable single-page application in React Keyframes Tool - Command line tool which convert CSS Animations to JavaScript objects gibbok.coding📧gmail.com
Updated on April 07, 2021Comments
-
GibboK about 3 years
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>
-
GibboK over 11 yearsIt seems really verbose, why do not use just Html.Raw ??
-
Rory McCrossan over 11 years@GibboK
MvcHtmlString.Create()
is more semantic IMO, although either solution will work for you. -
Andy Raddatz about 10 yearsYou'll want to be careful with that one, it blindly trusts the text and puts it to the page as a string. What if Model.ContentBody was equal to
<script> window.location.href="badGuyNeighborhood.com" </script>
-
xrx215 almost 9 years@Html.Raw(HttpUtility.HtmlDecode(@model.ContentBody)); This code worked for me. Thanks.
-
Mohit Kumar almost 9 years@xrx215 glad if it helps you :)
-
GibboK about 8 years@Andy Raddatz thanks for your tip, in my case all content is sanitized and safe from db.
-
user3654055 almost 8 yearsThe Html.Raw doesn't work for me -- however even still It's a security hazard for my purposes. The MvcHtmlString.Create does work, could you please explain any similarities to Html.Raw and whether or not the String.Create is more protective? (I need to populate a word from my model into a Javascript function)
-
blind Skwirl over 4 yearsfor hours I've been fiddling with CKEditor to get it to display the encoded text IT created to bypass ASP security, kinda crazy. @MvcHtmlString.Create(HttpUtility.HtmlDecode(@model.ContentBody)) finally worked!
-
Norbert Norbertson over 2 yearsRaw doesn't decode. So if your html looks like this:
<br/>
you will just see the HTML as text.