I'm using MVC 3 (the ASPX ViewModel) while I store and display data from my SQL database. I've tried using the raw input to store it as well as using HttpUtility.HtmlEncode. Neither are working when I try to display. I've tried using the HttpUtility.HtmlDecode as well as using <%: Model.MyHtmlVariable %>. Am I missing something?
-
"not working" is generally a bad description for a problem. You should post some code because all 3 of your attempted techniques should display something on that page.John Farrell– John Farrell2010-11-03 03:56:59 +00:00Commented Nov 3, 2010 at 3:56
-
I would think it would be obvious enought that I'm not getting HTML. The title does say displaying HTML from a database.Jason N. Gaylord– Jason N. Gaylord2010-11-03 12:52:15 +00:00Commented Nov 3, 2010 at 12:52
-
Well it wasn't obvious to me. At first glance this reads like MyHtmlVariable simply isn't populated. Just think of how clearer "is outputting encoded HTML" is vs "not working".John Farrell– John Farrell2010-11-03 17:40:34 +00:00Commented Nov 3, 2010 at 17:40
-
Fair enough. Thanks for the constructive criticism. :)Jason N. Gaylord– Jason N. Gaylord2010-11-05 18:37:48 +00:00Commented Nov 5, 2010 at 18:37
Add a comment
|
2 Answers
Using the traditional "<%= html %>" syntax should render it out for you but may not depending on what you're doing. If not, try to wrap it in an HtmlString object, like so:
<%= new HtmlString(html) %>
MVC should respect that and render it out properly.
If you're just looking to display the encoded HTML, the "<%: html %>" syntax is your friend
1 Comment
Jason N. Gaylord
Thanks Jess! Also, someone had pointed out to use a method that resturns an IHtmlString to keep consistency of the inline code.