如何从 ASP.NET MVC 视图显示存储在数据库中的 HTML?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/300424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display HTML stored in a database from an ASP.NET MVC view?
提问by Jedidja
I have HTML code emitted by FCKEditor stored in a database and would like to display (well render) it onto a view. So, for instance, something stored as:
我将 FCKEditor 发出的 HTML 代码存储在数据库中,并希望将其显示(很好地呈现)到视图上。因此,例如,存储为:
<>pre<>This is some sample text<>pre</>
Will be displayed to the user as:
将显示给用户:
This is some sample text
(With the appropriate style for pre-formatted-text)
(使用适当的预格式化文本样式)
The view already has the required string to display from ViewData
, I'm just not sure what the best way to show it to the user is.
该视图已经具有从 显示所需的字符串ViewData
,我只是不确定向用户显示它的最佳方式是什么。
回答by Pure.Krome
try
尝试
<%= System.Web.HttpUtility.HtmlDecode(yourEncodedHtmlFromYouDatabase) %>
more info here @ MSDN online.
更多信息在这里@ MSDN在线。
hth!
嗯!
回答by whoblitz
The answer provided by Pure.Krome is flawless for MVC2, but consider Razor syntax:
Pure.Krome 提供的答案对于 MVC2 来说是完美无缺的,但请考虑 Razor 语法:
@Html.Raw(System.Web.HttpUtility.HtmlDecode(Model.yourEncodedHtmlFromYourDatabase))
Alternatively,
或者,
@Html.Raw(Server.HtmlDecode(Model.yourEncodedHtmlFromYourDatabase))