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{
...
protectedoverridevoid InitializePage()
{
...
var requestedUrl = Request.Url.PathAndQuery.ToString(); // default get back to same page after valid login;
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'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.
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().
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):
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??
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.
HTML i'm passing to Html.Raw() is valid - I've checked that.
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.
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??
Try something like this:
What is that image? I'm running into this issue right now.
Hi Peter,
Can you post your code?
Thanks
//(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
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.
Thanks :). Glad you fouund a fix
For me this worked:
-cheers
Perfect! Works for me!
IHTMLString mystring = new HTMLString(html stuff);
is working on a reply...