Anthony Potts
Anthony Potts

Reputation: 9160

How does one pass html to page in View through ViewData in ASP.NET MVC?

Okay, so maybe this is a no no in the MVC pattern in which case, that's the answer I'm looking for. However, let's say I have some content html in a database and I want to pass it through to the view. I am assuming I could use a ViewData property to pass this through to the page. What sort of massaging do I need to do to the string to get something like <h1>Hello World</h1> to show up as 'Hello World' instead of '<h1>Hello World</h1>'.

Upvotes: 2

Views: 3372

Answers (2)

Matthew Groves
Matthew Groves

Reputation: 26141

You'd need to strip out the HTML before you put the string into the ViewData.

EDIT: When you print the ViewData directly to the page, it shouldn't escape the HTML markup.


<%=ViewData["whatever"]%>

That's C#, but it's very similar in VB.NET.

Upvotes: -1

Svante Svenson
Svante Svenson

Reputation: 12488

Nothing, just do <%=ViewData["yourkey"] %> instead of <%=Html.Encode(ViewData["yourkey"]) %> in your view.

Note: Only do this if you are absolutely sure you trust the data in the database!

Upvotes: 1

Related Questions