Copied to clipboard

Flag this post as spam?

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


  • Greg Berlin 818 posts 634 karma points
    Sep 18, 2012 @ 02:16
    Greg Berlin
    0

    Struggling with Html.Raw - Macro Outputting Html Encoded value

    I have a macro in which i'm building up a message from a few parameters on a node.  However when i output it (using the variable that i built up containing all the HTML), it renders the encoded value, IE: I see the HTML on the page, not the rendered HTML.

    Here's my code (a snippet of it anyway):

    @if (showMessage)

    {

        //@(Html.Raw(message.ToString()));

        @message;

    }

     

    @functions{

    ...

      protected override void InitializePage()

        {

    ...

            var requestedUrl = Request.Url.PathAndQuery.ToString(); // default get back to same page after valid login;

            if (isLoggedIn)

            {

                if (!isSubmitLogout)

                {

                    message = String.Format("<div class=\"header\">{0}</div>", @Model.signedInConfirmationHeader);

                    message = String.Concat(message, String.Format("<p>{0}</p>", @Model.signedInConfirmationMessage)); //Html.Raw(@Model.signedInConfirmationMessage)

                    message = message.Replace("%USER%", currentUser.UserName);

                    try

                    {

                        message = Html.Raw(message);

                    }

                    catch (Exception ex)

                    {

                        message = String.Format("ERROR: {0}", ex.Message);

                    }

                    showMessage = true;

     

    So in the above code, it errors. It doesn't even handle the exception and show the message, i just get a message saying there was an error with the script.

    If i remove the Html.Raw(..) block, it'll work but not render the HTML.

    I'm pretty stumped with this.. any ideas??

  • Greg Berlin 818 posts 634 karma points
    Sep 18, 2012 @ 02:24
    Greg Berlin
    0

    I've also tried every combination i can think of of @Html.Raw(message) etc in the top section where it actually outputs the value. All just throws a generic error.

    Also i've tried changing the "signedInConfirmationMessage" property to a Textbox Multiple - same behaviour.

    HTML i'm passing to Html.Raw() is valid - I've checked that.
    UGH!!
  • dan 29 posts 53 karma points
    Sep 18, 2012 @ 21:13
    dan
    0

    Maybe try setting it up like this. 

      @{
        var showMessage = true;
        var message = "<div>Test Message</div>";
        if (showMessage) {
          @Html.Raw(message)
        }
      }

     

    Did you try placing the function before the output?

     

    Sorry I can't be much more help.

  • Greg Berlin 818 posts 634 karma points
    Sep 18, 2012 @ 23:45
    Greg Berlin
    0

    Thanks Dan... not a bad idea to isolate it, makes it a bit clearer where the issue is:

    This throws an error:

    @if (showMessage)

    {

        //@(Html.Raw(message.ToString()));

        //@message;

     

        var message2 = "<div>Test Message</div>";

        @Html.Raw(message2);

        //@message2;

    }

     

    But this outputs the html encoded text to screen (ie: shows the markup on the front end):

    @if (showMessage)

    {

        //@(Html.Raw(message.ToString()));

        //@message;

     

        var message2 = "<div>Test Message</div>";

        //Html.Raw(message2);

        @message2;

    }

     

    So i think we can rule out the use of the function, or the placement of it, from this issue.  The issue is with building up a varialble containing HTML text, and then outputting it to the screen using Html.Raw().  

    Anyone??

  • Przemysław Siekierko 1 post 21 karma points
    Sep 19, 2012 @ 01:25
    Przemysław Siekierko
    0

    Try something like this:

  • Peter 2 posts 22 karma points
    Apr 10, 2013 @ 21:57
    Peter
    0

    What is that image?  I'm running into this issue right now.

  • dan 29 posts 53 karma points
    Apr 10, 2013 @ 22:17
    dan
    0

    Hi Peter,

    Can you post your code?

    Thanks

  • Charles Afford 1163 posts 1709 karma points
    Apr 10, 2013 @ 22:41
    Charles Afford
    0

        //(Html.Raw(message.ToString()));   <<<<<<<<<<<<<< THIS BIT!!!  you have a ( around the front of HTML.Raw //this will not help :)

        //message;

     

        var message2 = "<div>Test Message</div>";

        @Html.Raw(message2);

        //@message2;

    }

     

    also i think you should use @{    } around you code and not  @ @ @ on every line :).  Charlie

     

  • Peter 2 posts 22 karma points
    Apr 10, 2013 @ 22:58
    Peter
    0

    Turns out the issue was with is in 4.9.0, only with MVC Macros.  Switching to a User Control macro solved the issue on this version.

  • Charles Afford 1163 posts 1709 karma points
    Apr 15, 2013 @ 21:19
    Charles Afford
    0

    Thanks :).  Glad you fouund a fix

  • Zihadul Azam 26 posts 171 karma points c-trib
    May 14, 2019 @ 11:59
    Zihadul Azam
    1

    For me this worked:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var myString = Model.MacroParameters["param_alias"].ToString();
    }
    
    @Html.Raw(HttpUtility.HtmlDecode(myString))
    

    -cheers

  • ChristophC 21 posts 60 karma points
    Jul 16, 2020 @ 08:39
    ChristophC
    0

    Perfect! Works for me!

  • Ken Schnell 35 posts 147 karma points
    Jul 16, 2020 @ 09:13
    Ken Schnell
    0

    IHTMLString mystring = new HTMLString(html stuff);

Please Sign in or register to post replies

Write your reply to:

Draft