I have a string, read from a database, that contains HTML that I want to output. Despite applying HttpUtility.HtmlDecode(), the View always renders the string as encoded HTML (i.e. <SPAN> instead of <SPAN>).
I am using:
string test = WebUtility.HtmlDecode(myStr);
<span>#test</span>
I have tried:
string test = HttpUtility.HtmlDecode(myStr);
<span>#test</span>
<span>#HttpUtility.HtmlDecode(myStr)</span>
Use Html.Raw()
#Html.Raw("<span>Hello</span>")
All the output from helpers and other elements in Razor are put through HttpUtility.HtmlEncode, unless they implement IHtmlString. But your best option here is using Html.Raw()
You need to use #Html.Raw:
#Html.Raw("<h1>Header</h1>")
Will output the text Header.
Try this helper method
#Html.Raw(myStr)
Related
Suppose I have a string <span class="msg">Text goes here</span>.I need to use this string as a HTML element in my webpage. Any ideas on how to do it?
Mithril provides the m.trust method for this. At the place in your view where you want the HTML output, write m.trust( '<span class="msg">Text goes here</span>' ) and you should be sorted.
Mithril it's powerfull thanks to the virtual dom, in the view you if you want to create a html element you use:
m("htmlattribute.classeCss" , "value");
So in your case:
m("span.msg" , "Text goes here");
Try creating a container you wish to hold your span in.
1. Use jQuery to select it.
2. On that selection, call the jQuery .html() method, and pass in your HTML string.
($('.container').html(//string-goes-here), for example)
You should be able to assign the inner HTML of the container with the string, resulting in the HTML element you want.
Docs here.
Suppose I have a string <span class="msg">Text goes here</span>.I need to use this string as a HTML element in my webpage. Any ideas on how to do it?
Mithril provides the m.trust method for this. At the place in your view where you want the HTML output, write m.trust( '<span class="msg">Text goes here</span>' ) and you should be sorted.
Mithril it's powerfull thanks to the virtual dom, in the view you if you want to create a html element you use:
m("htmlattribute.classeCss" , "value");
So in your case:
m("span.msg" , "Text goes here");
Try creating a container you wish to hold your span in.
1. Use jQuery to select it.
2. On that selection, call the jQuery .html() method, and pass in your HTML string.
($('.container').html(//string-goes-here), for example)
You should be able to assign the inner HTML of the container with the string, resulting in the HTML element you want.
Docs here.
Here's an example of what I'm inputting into my creation form:
I'd like to somehow convert that input into actual HTML when rendering in my view like:
<p>+65 Magical Power</p>
<p>+75 Mana</p>
<p>+10 MP5</p>
I tried to create a helper method in the Item class. But it seems I cannot use the content_tag method in the Models or Controllers.
What would be a good way to solve this problem?
I think <xmp></xmp> can be used. I tried using it with simple <p> </p> tag but did not try echoing the input field.
in my JSON string (from the server response) I have a HTML tags, eg abc<br/>dd. That string goes to the DOM element as its content.
How to display it as a proper HTML, instead of raw string ?
You'll need to set it as the HTML rather than the text. Using DOM directly, you'd set element.innerHTML = myValue;. If you're using jQuery, you'd use $('#myElement').html(myValue);.
I've a String field on my model that contains an html value, my problem is that when I place the code to render this field on my view it will be entirely reconverted to html, and the final result is a big string with html characters escaped...
#field //= "<div>"
renders
< ;div> ;
How can i override this behavior and force it to write the html unescaped on my field?
You could use the Html.Raw helper:
#Html.Raw(field)
Use #Html.Raw:
#Html.Raw(field)