HTML 编码解码 c# MVC4
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9050145/
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
HTML encode decode c# MVC4
提问by Gokul
I am in a process of upgrading a c# MVC2 project into c# MVC4.
我正在将 ac# MVC2 项目升级到 c# MVC4。
Here is the scenario in MVC2
这是MVC2中的场景
Input string(from database)
输入字符串(来自数据库)
Model.text="<p>Hi<br>hello!<br>you there</p>"
Output (rendered in the view) rendered using
使用渲染的输出(在视图中渲染)
<%=Model.text %>
Hi
hello!
you there
Here is the scenario in MVC4
这是MVC4中的场景
Input string(from database)
输入字符串(来自数据库)
Model.text="<p>Hi<br>hello!<br>you there</p>"
Output (rendered in the view) rendered using
使用渲染的输出(在视图中渲染)
@Model.text
<p>Hi<br>hello!<br>you there</p>
I tried
我试过
@HttpUtility.HtmlDecode(Model.text)
@HttpUtility.HtmlEncode(Model.text)
Nothing helps...
没有什么帮助...
I had a similar problem in MVC4 asked here(the ajax result is rendered with html tags not the actual html)
我在这里询问了 MVC4 中的类似问题(ajax 结果是用 html 标签呈现的,而不是实际的 html)
Is some of my settings troubling me??? or is that something to do with HTML 5 or am I missing anything in using MVC4. Please help!!
我的某些设置是否让我感到困扰???或者这与HTML 5有关还是我在使用MVC4时遗漏了任何东西。请帮忙!!
采纳答案by Justin Pihony
This should do the trick:
这应该可以解决问题:
@Html.Raw(Model.text)
回答by Serkan Inci
If you don't want your text get encoded, that text should be of type IHtmlString. String texts are encoded by default.
如果您不想对文本进行编码,则该文本的类型应为 IHtmlString。默认情况下对字符串文本进行编码。
In your case,
在你的情况下,
Model.text = MvcHtmlString.Create("<p>Hi<br>hello!<br>you there</p>");
would do the trick as well.
也会这样做。
回答by sanket parikh
In controller side
在控制器端
viewbag.msg="hello";
in the html.cs razor view
在 html.cs 剃刀视图中
@Html.Raw(viewbag.msg)

