ASP.NET MVC从控制器传递原始HTML查看 [英] ASP.NET MVC Passing Raw HTML from Controller to View

查看:266
本文介绍了ASP.NET MVC从控制器传递原始HTML查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在抓我的头这个几天,我不知道这是否是我的环境或正在为ASP.NET MVC(此基础代码本身的问题,虽然我有5年工作经验C#)。我使用所有的补丁Win7x64和VS 2008的近期干净安装。

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.

我原始的HTML存储在有选择地基于控制器加载数据库表我不拥有控制权的一些规则。不幸的是,当尝试的东西,在像这样的控制值到视图数据:

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();

当我看到输出,它被转义/ HTML编码。我试着用以下所有这一切似乎并没有解决这个问题:

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())%>

和...

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

和...

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



...它抓住从数据库表中的原始的HTML就好了,但是它保留了强制爆破不管我怎么努力编码。从我对MSDN阅读,有大约包含空格从HTML中没有被正确地解码产生的问题(这确实雷)注脚。因为我怀疑我是谁一直面临着这个我转向你的乡亲一些想法只有一个。

...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.

我即将杂牌我的方式,虽然它与正则表达式视图做网页清理,但认为这将是最好先得到一些其他人一些建议之前,我提前蛮力it.Thanks。

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.

推荐答案

<%:办法的编码,如果必要的。如果你不希望出现这种情况,那么的的办法是使用<%= ,但坦白说,我建议你,而不是包装在 IHtmlString ,例如:

<%: 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);

现在,如果你存储的的和表现出来,它应该采取HTML原样。

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

这篇关于ASP.NET MVC从控制器传递原始HTML查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆