Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC2: Is there an Html Helper for raw Html?

Is there an Html helper that simply accepts and returns raw html? Rather than do something ugly like this:

<% if (Model.Results.Count > 0) { %><h2>Results</h2><% } %>

I'd like to do something like this:

 <% if (Model.Results.Count > 0) { Html.RawHtml("<h2>Results</h2>") } %>

Not a whole lot cleaner, but I think it's a bit of an improvement. Does something like that exist? Or is there perhaps a better alternative to output raw html from within those escape characters than using Html helpers?

like image 523
Mike Pateras Avatar asked Jan 30 '10 22:01

Mike Pateras


People also ask

What is HTML raw helper?

The Html. Raw Helper Method is used to display HTML in Raw format i.e. without encoding in ASP.Net MVC Razor.

Can we use HTML Raw?

Raw method does not work and I have to use HttpUtility. HtmlDecode(EncodedContent) before I use Html.

Which built in HTML helper is used to render a HTML link?

Html. ActionLink() Helper is used to render an HTML link in a View. ActionLink() method actually links to a Controller action from inside a View.


3 Answers

For MVC2:

<%: MvcHtmlString.Create("<h2>Results</h2>") %>

Found here:

store and display html tags in MVC

like image 55
Michael Maddox Avatar answered Oct 14 '22 06:10

Michael Maddox


Response.Write should work. (Although maybe it's kind of taking a step back!) You should be able to create an extension method to do it. And maybe instead of using HTML string, you might want to build your markup in code using the TagBuilder.

like image 5
ziya Avatar answered Oct 14 '22 06:10

ziya


There is such helper now:

Html.Raw("<h2>Results</h2>")
like image 1
BlackTigerX Avatar answered Oct 14 '22 06:10

BlackTigerX