Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Passing Raw HTML from Controller to View

I have been scratching my head about this for a few days, and I am not sure if it is an issue with my environment or the code itself basing this on being to ASP.NET MVC (although I have 5 years experience in C#). I am using a recent clean install of Win7x64 and VS 2008 with all the patches.

I have raw HTML stored in a database table that is selectively loaded by the controller based on a few rules which I do not have control over. Unfortunately when attempt to stuff the value into a view data in the control like such:

ViewData["HTMLData"] = DAO.HTMLDataGet();

When I see the output, it is escaped/HTML Encoded. I tried using the following all of which did not seem to resolve this issue:

<%: HttpUtility.HtmlDecode(ViewData["HTMLData"].ToString())%>

And...

<%: Server.HtmlDecode(ViewData["HTMLData"].ToString())%>

And...

<%: Html.Raw(ViewData["HTMLData"].ToString())%>

...it grabs the raw HTML from the database table just fine, however it keeps forcing that blasted encoding regardless of what I try. From what I read on the MSDN, there was a foot note about problems resulting from HTML not being decoded properly that contained spaces (which mine does). Since I doubt I am the only one who has faced this I am turning to you folks for some ideas.

I am about to Kludge my way though it with a regex in the view to do page cleanup, but thought it would be better to get some advice from some other folks first before I brute force it.Thanks in advance.

like image 926
Dave Avatar asked Aug 20 '12 07:08

Dave


People also ask

How do I pass HTML content from controller view?

You can use ViewBag. YourField or ViewData["YourStringName"] and for retreiving it in your View. Just place it where you want preceded by @ like this @ViewBag. YourField or @ViewData["YourStringName"] .

How can we pass the data from controller to view in MVC?

The other way of passing the data from Controller to View can be by passing an object of the model class to the View. Erase the code of ViewData and pass the object of model class in return view. Import the binding object of model class at the top of Index View and access the properties by @Model.

How do you pass data from controller action to view pages?

ViewBag is a very well known way to pass the data from Controller to View & even View to View. ViewBag uses the dynamic feature that was added in C# 4.0. We can say ViewBag=ViewData + Dynamic wrapper around the ViewData dictionary.

Can we pass TempData from controller to view?

TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class.


1 Answers

<%: means "encode if necessary". If you don't want that, then the lazy approach would be to use <%=, but frankly I suggest you instead wrap it in IHtmlString, for example:

string yourEncodedHtml = ...
var html = new MvcHtmlString(yourEncodedHtml);

Now, if you store that and show it, it should take the html "as is".

like image 102
Marc Gravell Avatar answered Oct 21 '22 00:10

Marc Gravell