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

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

问题描述

我使用Html.Raw打印原始的html内容,例如,当我从控制器向视图端发送ViewBag.div = "<div> Hello </div>";之类的东西时,除非我使用Html.Raw方法,否则它不会显示原始的html内容.但是,如果我有编码内容,例如使用jquery编码并插入数据库的内容,并且想将其打印为原始html内容,则Html.Raw方法不起作用,必须先使用HttpUtility.HtmlDecode(EncodedContent),然后再使用因此,请任何人解释为什么它会以这种方式起作用,以及使用Html.Raw方法的正确情况是什么?或以另一种方式,为什么Html.Raw在接收html实体而不是html标签作为参数时不起作用?

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

推荐答案

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

Html.Raw呈现给定的内容,而无需进行任何html编码,因此使用ViewBag.div = "<div> Hello </div>";:

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

@Html.Raw(ViewBag.div);

渲染器

<div> Hello </div>

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

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.

如果Html.Raw确实进行了解码,则将造成混乱,并且我们需要做一些没有做的事情. ;-)

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天全站免登陆