Html.Raw MVC 助手如何工作? [英] How does Html.Raw MVC helper work?

查看:16
本文介绍了Html.Raw MVC 助手如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Html.Raw 打印原始 html 内容,例如当我从控制器到视图端它不会打印原始 html 内容,除非我使用 Html.Raw 方法但如果我有一个编码的内容,比如使用 jquery 编码并插入到数据库中的内容,我想将其打印为原始 html 内容,Html.Raw 方法不起作用,我必须在使用 Html.RawHttpUtility.HtmlDecode(EncodedContent) 之前使用 HttpUtility.HtmlDecode(EncodedContent)/code> 所以请谁能解释一下为什么它会以这种方式运行以及使用 Html.Raw 方法的正确情况是什么?或者换句话说,为什么 Html.Raw 在它接收 html 实体作为参数而不是 html 标签时不起作用?.

解决方案

因为编码的字符是 HTML,而该字符串的原始版本是编码的字符.

<小时>

Html.Raw 在不进行任何 html 编码的情况下呈现给定的内容,因此使用 ViewBag.div = "

Hello </div>";:

@Html.Raw(ViewBag.div);

渲染

你好

但是,当您在其中编码字符时,例如 ViewBag.Something = "&gt;"; 原始版本是 &gt;.要返回实际的 html,您需要 Html.Raw(HttpUtility.HtmlDecode(EncodedContent)); 正如您所说的那样.

如果 Html.Raw 确实进行了解码,那么它会令人困惑,我们将需要一些没有做到的东西.;-)

I use the Html.Raw to print a raw html content, for example when I send some thing like ViewBag.div = "<div> Hello </div>"; from the controller to the view side it does not print a raw html content unless I use the Html.Raw method but if I have an encoded content, like content encoded using jquery and inserted into the database and I want to print it as a raw html content the Html.Raw method does not work and I have to use HttpUtility.HtmlDecode(EncodedContent) before I use Html.Raw so please could anyone explain why it acts in this way and what is the proper situation to use Html.Raw method? or in another way, why Html.Raw does not work when it receives html entities as a parameter instead of html tags?.

解决方案

Because encoded characters are HTML, and the Raw version of that string is the encoded one.


Html.Raw renders what it is given without doing any html encoding, so with ViewBag.div = "<div> Hello </div>";:

@Html.Raw(ViewBag.div);

Renders

<div> Hello </div>

However, when you have encoded characters in there, such as ViewBag.Something = "&gt;"; the raw version of that is &gt;. To get back to actual html you need to Html.Raw(HttpUtility.HtmlDecode(EncodedContent)); as you've said.

If Html.Raw did do the decoding then it would be confusing, and we would need something that didn't do it. ;-)

这篇关于Html.Raw MVC 助手如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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