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

73,093

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 = "&lt;p&gt;&lt;span style=&quot;font-size: small; color: #ff0000;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&amp;nbsp;&lt;span style=&quot;font-size: large; color: #000000;&quot;&gt;Hi&lt;/span&gt;&lt;br /&gt;&amp;nbsp; &lt;br /&gt;This is just a sample,&lt;br /&gt;This will not work with @Html.Raw(),&lt;br /&gt;";
  <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)   
Share:
73,093
GibboK
Author by

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, 2021

Comments

  • GibboK
    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
    GibboK over 11 years
    It seems really verbose, why do not use just Html.Raw ??
  • Rory McCrossan
    Rory McCrossan over 11 years
    @GibboK MvcHtmlString.Create() is more semantic IMO, although either solution will work for you.
  • Andy Raddatz
    Andy Raddatz about 10 years
    You'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
    xrx215 almost 9 years
    @Html.Raw(HttpUtility.HtmlDecode(@model.ContentBody)); This code worked for me. Thanks.
  • Mohit Kumar
    Mohit Kumar almost 9 years
    @xrx215 glad if it helps you :)
  • GibboK
    GibboK about 8 years
    @Andy Raddatz thanks for your tip, in my case all content is sanitized and safe from db.
  • user3654055
    user3654055 almost 8 years
    The 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
    blind Skwirl over 4 years
    for 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.ContentB‌​ody)) finally worked!
  • Norbert Norbertson
    Norbert Norbertson over 2 years
    Raw doesn't decode. So if your html looks like this: &lt;br/&gt; you will just see the HTML as text.