asp.net-mvc 从控制器返回 html 字符串并在视图中显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10661561/
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-09-08 01:57:33  来源:igfitidea点击:

Return html string from controller and display in view

asp.net-mvcasp.net-mvc-3

提问by MuriloKunze

How can I return a model with a string propertie containing <li>elements and display it in view? If I just write @Model.Messages it shows all the string.. i need it in html format.

如何返回具有包含<li>元素的字符串属性的模型并将其显示在视图中?如果我只写 @Model.Messages 它会显示所有字符串..我需要它的 html 格式。

采纳答案by web_bod

You don't say which rendering engine you're using:

你没有说你使用的是哪个渲染引擎:

MVC3:
@Html.Raw(Model.Description)

MVC3:
@Html.Raw(Model.Description)

回答by Kim Tranjan

You can use the Contentmethod with the Content-Type text/htmlto return the HTML directly, without the need of Html.Raw.

您可以使用Content带有 Content-Type的方法text/html直接返回 HTML,而无需Html.Raw.

public ActionResult MyHtmlView() {
    return Content("<html><body>Ahoy.</body></html>", "text/html")
}

You can pass whatever Content-Type you want, such text/xml.

您可以传递您想要的任何内容类型,例如text/xml.

回答by Niranjan Singh

Use Server.HtmlEncode()to send html to view and then use the Server.HtmlDecode()to get the html to display on the view.

使用Server.HtmlEncode()发送HTML到视图,然后用Server.HtmlDecode()得到的html显示屏上的视图。

Then you can use @Html.Raw(Server.HtmlDecode(str)).

然后你可以使用 @Html.Raw(Server.HtmlDecode(str)).

Try this:

尝试这个:

<div class='content'>     
   @Html.Raw(HttpUtility.HtmlDecode(Model.Message)); 
</div> 

Ref: Display encoded html with razor

参考: 使用剃刀显示编码的 html