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 176 posts 441 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?

  • Tim Geyssens 6562 posts 15373 karma points MVP 2x c-trib
    Feb 23, 2016 @ 16:43
    Tim Geyssens
    1

    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 176 posts 441 karma points
    Feb 23, 2016 @ 17:39
    Thomas Beckert
    0

    Using 7.4 passing the code by macro Parameter Editor textarea.

  • Thomas Beckert 176 posts 441 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()))
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies