Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Richard Terris 273 posts 715 karma points
    Oct 10, 2013 @ 17:42
    Richard Terris
    0

    Rendering HTML in Razor - Umbraco 6.1.6

    Hi everyone,

    Wherever I enter HTML tags in properties and try to display them using razor, it is rendering correctly as end user is concerned but upon viewing the source I can see that the HTML seems to be being decoded.

    This is a problem for a couple of reasons:

    • Keywords: <meta name="keywords" content="&lt;p&gt;Keyword, another word, something silly&lt;/p&gt;" />
    • Plugins - I have a 3rd party Twitter plugin and the HTML is in the document Type property. My razor simply grabs this property value and displays it, except that every tag is decoded and therefore the plugin doesn't work

    I've tried various methods such as Html.Raw, Html.Encode and even Html.Raw(Html.Encode( as suggested in a blog post but nothing works.

    Any suggestions?

    `Thanks in advacnce.

  • Bear 40 posts 129 karma points
    Oct 10, 2013 @ 17:52
    Bear
    0

    Could you use @Library.StripHtml before you render it?

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Oct 10, 2013 @ 17:54
    Peter Duncanson
    0

    Give us a code snippet and what does your actual data too look?

    The keyword one is legit surely? You just want a strip HTML method to bin the p tags out.

    Pete

  • Bear 40 posts 129 karma points
    Oct 10, 2013 @ 18:01
    Bear
    0

    Or a combination @Html.Raw( Library.StripHtml( string ) )?

  • Richard Terris 273 posts 715 karma points
    Oct 10, 2013 @ 18:05
    Richard Terris
    0

    I see your point with regards the keywords, although for some reason I can't access Library from the page. It's the _Layout.cshtml page which inherits from UmbracoTemplatePage, as opposed to inheriting from MacroEngines - is that the issue?

    Regarding the other issue as the code currently stands:

        @inherits umbraco.MacroEngines.DynamicNodeContext
    @using umbraco.MacroEngines;
    
        @foreach (var item in Model.componentSelector)
        {
                var newNode = new umbraco.NodeFactory.Node(Convert.ToInt32(item.InnerText));
                @Library.StripHtml(newNode.GetProperty("pluginHTML").Value);   
        }
    

    The pluginHTML property contains:

    <div style="width:236px;font-size:8px;text-align:right;"><script type="text/javascript">
    document.write(unescape("%3Cscript src='http://twitterforweb.com/twitterbox.js?username=myusername&settings=1,1,3,236,550,f4f4f4,0,c4c4c4,101010,1,1,336699' type='text/javascript'%3E%3C/script%3E"));</script>Created by: <a href="http://twitterforweb.com" target="_blank">twitter website widget</a></div>
    
  • Richard Terris 273 posts 715 karma points
    Oct 10, 2013 @ 18:07
    Richard Terris
    0

    I should say the above code is a razor script called by a Macro.

    I'd agree with any points about using partial views but this is a project I'm building as a kind of starter kit for front end developers who are used to macros and have no clue about partial views

    I refuse to do it in web forms though, which was also suggested :D

  • Richard Terris 273 posts 715 karma points
    Oct 10, 2013 @ 18:10
    Richard Terris
    0

    Well done John Bear - @Html.Raw(library.StripHtml()) works for the keywords and description.

    This solution won't work in the other situation because I don't want to remove the HTML

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Oct 10, 2013 @ 18:19
    Peter Duncanson
    100

    Html.Raw should work for your second issue. Most odd. More digging required I think mate :(

  • Richard Terris 273 posts 715 karma points
    Oct 10, 2013 @ 18:31
    Richard Terris
    0

    Maybe, but doesn't :(

    I have also tried this as Textbox Multiple and TextString data types in case it was TinyMCE adding it's own padding.

    Since this is a node being referenced by MTNP I also wondered if something was happening to the HTML so I added the same HTML to a different property on the current page and used @Model - same outcome

  • Richard Terris 273 posts 715 karma points
    Oct 11, 2013 @ 10:34
    Richard Terris
    0

    Weird - I already tried this but I again changed the property to a TextboxMultiple instead of Rich Text Editor and used @Html.Raw() and it now works!

    Thanks for the help and suggestions guys!

Please Sign in or register to post replies

Write your reply to:

Draft