如何从 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 22:45:24  来源:igfitidea点击:

How to display HTML stored in a database from an ASP.NET MVC view?

htmlasp.net-mvchtml-encode

提问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</&gt

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

回答by Nerdroid

you want to use @Html.Raw(str)

你想用 @Html.Raw(str)

See MSDNfor more

请参阅MSDN更多

Returns markup that is not HTML encoded.

This method wraps HTML markup using the IHtmlString class, which renders unencoded HTML.

返回非 HTML 编码的标记。

此方法使用 IHtmlString 类包装 HTML 标记,该类呈现未编码的 HTML。