从控制器返回html字符串并在视图中显示

Mur*_*nze 6 asp.net-mvc asp.net-mvc-3

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

Kim*_*jan 15

您可以将该Content方法与Content-Type text/html一起使用,直接返回HTML,而无需使用Html.Raw.

public ActionResult MyHtmlView() {
    return Content("<html><body>Ahoy.</body></html>", "text/html")
}
Run Code Online (Sandbox Code Playgroud)

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


web*_*bod 7

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

MVC3:
@Html.Raw(Model.Description)


Nir*_*ngh 5

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

那你可以用 @Html.Raw(Server.HtmlDecode(str)).

尝试这个:

<div class='content'>     
   @Html.Raw(HttpUtility.HtmlDecode(Model.Message)); 
</div> 
Run Code Online (Sandbox Code Playgroud)

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