Render HTML with MVC ViewBag - asp.net-mvc

Is there a way to render HTML tags from a ViewBag? I am pulling article content out of a database, but the HTML tags included in the article aren't rendered.
For example:
ViewBag.ArticleContent = "Machine <strong>Language</strong> Content";
The tag doesn't render, it just shows up as text. This is a static string, but I get the same issue when I assign to ViewBag.ArticleContent from the database.

I figured it out. This appears to be working:
#Html.Raw(ViewBag.ArticleContent)

Related

Render Domain Field as HTML in GSP?

I have a domain. User with a field description. In my Grails GSP page I can display it as:
${user.description}
The problem is that description contains valid html content like this:
<ul><li>test</li><li>test2</li><li>test3</li></ul><br>
This content should be rendered to be visible as HTML instead of a String.
How can I render a String containing HTML as HTML on a GSP?
To generate raw output use any of the following:
${raw(user.description)}
or
${user.description.encodeAsRaw()}
You can read this blog post that details this further. In later versions the default codec being used is HTML. You can control the default codec via Config.groovy.

Links not displaying properly in ASP MVC3 razor view

I've implemented the solution explained in this post How to get tweet's HTML with LinqToTwitter? but when I display my tweets the HTML links appear like this
<a class="inline" href="http://twitter.com/cgitosh" target="_blank">#cgitosh</a> And how are you?
instead of just showing #cgitosh And how are you? with #cgitosh linking to the twitter account.
What I'm I not doing right?
Razor code snipet:
#{var tweet = TwitterExtensions.Text2Html(item.Text);}
<div>#tweet</div>
So I basically pass the tweet text to the Text2HTML function which is explained in the link provided above which returns the tweet with links to the variable tweet which I then output in my view
Try:
#{var tweet = TwitterExtensions.Text2Html(item.Text);}
<div>#(new HtmlString(tweet))</div>
...and unless you're using tweet elsewhere, you could just do
<div>#(new HtmlString(TwitterExtensions.Text2Html(item.Text)))</div>
Razor by default HTML encodes strings, so you have to explicitly tell it to render it as markup. (See here.) Hope this helps!
Try like this:
<div>#Html.Raw(tweet)</div>
The Html.Raw method will not HTML encode the output which is what Razor does by default.

Why the content display code html without render it?

I have used MarkiItUp to build a jquery editor. Now when user type content in textarea with html code, like this:
A new <strong>true</strong> question
When record was saved, it save A new <strong>true</strong> question. And when it display on page, it also display like content was saved, is A new <strong>true</strong> question . But i want the content display on web like this:
A new true question
How can i do that?
To display it, use html_safe. This way your string would be rendered as HTML.
Refer to this question for more info.
Ruby on Rails: how to render a string as HTML?

Rendering html using Struts2

Using Struts2, my goal is to present a simple blog to a user using Struts2 iterators, such as:
Most Recent Topic
response 1
response 2
...
Previous Topic
response 1
response 2
...
Users generate and submit each Topic/Response using a separate form, but, once submitted, I don't want them to edit the blog.
To generate either a Topic or a Response, I provide an editor (like the stackoverflow editor I'm using now) that produces html-formatted text, including whatever styling (bold, underlines, lists, etc.) that the user chooses. The text of the Topic/Response created by the user, including the html tags, is stored in a database.
However, I cannot find a way to render the Topic/Response as html in the blog. For example, text bolded in the editor shows up as <strong>text</strong> in a struts2 s:textarea tag.
I know that the s:property tag has an 'escapeHtml' attribute that will prevent this, but the s:property tag can't layout the text properly, and it seems that only the s:property tag has this attribute.
I've tried using <input value="%{#topic.content}" /> within the iterator instead of s:textarea, but it doesn't seem to recognize the #topic iteration reference.
Is there a way to do this?
use text instated of tax area .Let me know if you still facing this issue.
Use escapeHtml="false". I just tried it myself and it works as intended.
For example, with:
<s:set var="var1"><p>some stuff</p><p>other stuff</p></s:set>
<s:property value="var1" escapeHtml="false" />
renders the paragraph tags as you would expect.
How about using <pre> with <s:property>.
About html <pre> tag:
http://www.w3schools.com/tags/tag_pre.asp

HTMl Helpers in MVC: How do I render html tags with helper such as ActionLink?

When using ActionLink to render data from database which has HTML tags
(ie <p>)
incorporated in it, ActionLink escapes the tags. What is the best way to handle this?
In valid (X)HTML, paragraph tags are disallowed inside of anchor tags, so I wouldn't expect the framework to allow it.
I don't know that you can turn off the XSS protection in the helper methods, but you can always build your own helper methods. Just make an extension method that hangs off the Html class.
If you just want to render some HTML from the database, you can use <%= ViewData["MyContent"] %> if you're controller loads the data into the MyContent view data. Just know that you have to clean this HTML yourself.

Resources