从问题说起
1. 记录在数据库的值,里面是一些有html布局元素。希望将其在页面上重新展示(比如上面有一张图片)。下面是记录在数据中的数据(htmlEncode后的代码)。
<p>改变测试<imgborder="0"src="http://www.quanbao.com/Content/ProductImages/2011-09/2011914160745634516132656150456.jpg"></p>
我在页面中输出时:(@Model.ServiceDetail)
发现没有输出展示的信息(比如里面有一<img>标签,它没有显示成一张图片)
<p>改变测试<imgborder="0"src="http://www.quanbao.com/Content/ProductImages/2011-09/2011914160745634516132656150456.jpg"></p>
解决:
@(new HtmlString(HttpUtility.HtmlDecode(Model.ServiceDetail)))
亦可
@ Html.Encode( HttpUtility.HtmlDecode(Model.ServiceDetail)
注意:
@(new HtmlString(HttpUtility.HtmlDecode(Model.ServiceDetail))) ――值被html encode了
@(new HtmlString(HttpUtility.HtmlDecode(Model.ServiceDetail)).ToHtmlString()),返回一个被html encode的串。二者是有区别的
思考
.....