I'm getting the following text from a database: (supplied by client, so I can't do much with it)
investment professionals.<BR /><BR /> blah blah blah
which is getting rendered as:
investment professionals.<BR /><BR /> blah blah blah
I don't want to print the <BR />
tags on the screen. I want them to behave as actual breaks.
The following Html Helper code builds the span it exists in, adds that to a div and returns the HTML string:
StringBuilder sbElements = new StringBuilder();
TagBuilder span = new TagBuilder("span") {InnerHtml = subject.AboutText};
sbElements.Append(span.ToString());
TagBuilder div = new TagBuilder("div");
div.MergeAttribute("class", "about-text");
div.InnerHtml = sbElements.ToString();
return div.ToString();
If I Html.Encode()
the output of the helper method, the encoded tags - /><
- get written to the screen. How can I take the source text I have and ensure that the tags get rendered as HTML, rather than text?
To render the html string in react, we can use the dangerouslySetInnerHTML attribute which is a react version of dom innerHTML property. The term dangerously is used here to notify you that it will be vulnerable to cross-site scripting attacks (XSS).
The Render FunctionThe ReactDOM.render() function takes two arguments, HTML code and an HTML element. The purpose of the function is to display the specified HTML code inside the specified HTML element.
Rendering tags are text strings embedded in the server response file HTML source code. A rendering tag has this general form: {{ tag }} {{ tag (parameter-list) }}
You can show HTML tags as plain text in HTML on a website or webpage by replacing < with < or &60; and > with > or &62; on each HTML tag that you want to be visible. Ordinarily, HTML tags are not visible to the reader on the browser.
If you are using Razor, it will doubly-encode your string as @Chevex described. If you use the new MVC3 <%: %>
syntax, it will also doubly-encode it. Regardless of your view engine, you can work around the encoding with either the IHtmlString route (e.g., MvcHtmlString) described by @Chevex or by bypassing the default encoding using a different template syntax.
The later, which doesn't involve changing any code, just requires tweaking the syntax you use to render to a view.
For Razor:
@Html.Raw(yourvariable)
For MVC's default view template system:
<%=yourvariable%>
Try this:
String Input = "investment professionals.<BR /><BR /> blah blah blah"; String Output = Server.HtmlDecode(Input);
Change return div.ToString()
to MvcHtmlString.Create(div.ToString())
and the method return type to MvcHtmlString
instead of a string
. This will prevent the view engine from automatically encoding the output.
Returning MvcHtmlString
is the standard practice for rendering HTML output via helper methods.
What is happening is that you are decoding the value from the database and the view engine is re-encoding it when it is rendered. The Razor view engine automatically HTML encodes output in your views unless it is an MvcHtmlString
. Returning MvcHtmlString
will stop that from happening.
public static MvcHtmlString MyNonHtmlEncodedOutput(this HtmlHelper html)
{
StringBuilder sbElements = new StringBuilder();
TagBuilder span = new TagBuilder("span") {InnerHtml = Server.HtmlDecode(subject.AboutText)};
sbElements.Append(span.ToString());
TagBuilder div = new TagBuilder("div");
div.MergeAttribute("class", "about-text");
div.InnerHtml = sbElements.ToString();
return MvcHtmlString.Create(div.ToString());
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With