Copied to clipboard

Flag this post as spam?

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


  • Ole Kristian Losvik 22 posts 73 karma points
    Jan 10, 2015 @ 13:56
    Ole Kristian Losvik
    0

    Raw html in razor macro

    I am trying to make a macro for rendering raw html in umbraco 7 with grid / fanoe. The macro has two parameters: htmlCode and cssClass.

    However the only output i get is the html code as text. Any suggestions?

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @if (Model.MacroParameters["htmlCode"] != null)
    {
        var htmlOutput = Model.MacroParameters["htmlCode"];
        string cssClass = "";
    if (Model.MacroParameters["cssClass"] != null)
    {cssClass = Model.MacroParameters["cssClass"].ToString();}
    @:<div class="@cssClass">
    @Html.Raw(htmlOutput.ToString());
    @:</div>
    }
  • Steve Morgan 1349 posts 4458 karma points c-trib
    Jan 10, 2015 @ 14:11
    Steve Morgan
    0

    Your code looks fine (I'm assuming you're looking to get the unescaped html Output rather than have it render it as HTML which is why you've used @Html.Raw)  - try changing it to this below to be sure - if this works then it's something to do with your parameter, can you post how you're passing this from the template? 

     var htmlOutput = "<p>Some test html</p>";
  • Ole Kristian Losvik 22 posts 73 karma points
    Jan 10, 2015 @ 18:55
    Ole Kristian Losvik
    2

    Thanks for your help, I am not sure why, but it seems like this works

     

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    @if (Model.MacroParameters["htmlCode"] != null)
    {
        var htmlOutput = Model.MacroParameters["htmlCode"];
        string cssClass = "";
    if (Model.MacroParameters["cssClass"] != null)
    {cssClass = Model.MacroParameters["cssClass"].ToString();}

        

    @:<div class="@cssClass">
    @Html.Raw(Server.HtmlDecode(htmlOutput.ToString()));
    @:</div>
    }
Please Sign in or register to post replies

Write your reply to:

Draft