Copied to clipboard

Flag this post as spam?

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


  • Thomas Beckert 193 posts 469 karma points
    Feb 23, 2016 @ 16:36
    Thomas Beckert
    0

    Render javascript-Code from String in Razor

    Hi,

    I have a Macro-Parameter where the user can insert Code-Snippets in the grid. Simple example:

    <script>alert("Hello World")</script>
    

    I tried to render this Snipet in Razor, but it is rendered with html-Escape-code:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @{
        @Html.Raw(Model.MacroParameters["Code"])
    }
    

    Result:

    &lt;script&gt;
    alert(&quot;hello world&quot;);
    &lt;/script&gt;
    

    Is there a way to render it, so that the code does its job?

  • Comment author was deleted

    Feb 23, 2016 @ 16:43

    Hmm Html.Raw should do the trick, the encoding probably happens in an earlier stage, will have to check

    Which Umbraco version are you running?

  • Thomas Beckert 193 posts 469 karma points
    Feb 23, 2016 @ 17:39
    Thomas Beckert
    0

    Using 7.4 passing the code by macro Parameter Editor textarea.

  • Thomas Beckert 193 posts 469 karma points
    Feb 23, 2016 @ 20:42
    Thomas Beckert
    0

    Thank you for the hint to the "earlier stage".

    This code does the job:

    @Html.Raw(HttpUtility.HtmlDecode(Model.MacroParameters["Code"].ToString()))
    
Please Sign in or register to post replies

Write your reply to:

Draft