如何在MVC的模型属性中呈现HTML标签

6 浏览
0 Comments

如何在MVC的模型属性中呈现HTML标签

我在数据库表中保存了安全/已过滤的HTML内容。

如何在Razor视图中输出这个HTML内容?

它总是转义一些字符,比如<和&符号变成了&

admin 更改状态以发布 2023年5月23日
0
0 Comments

在ASP.NET MVC 3中,应该这样做:\n

// Say you have a bit of HTML like this in your controller:
ViewBag.Stuff = "
  • Menu
  • " // Then you can do this in your view: @MvcHtmlString.Create(ViewBag.Stuff)

    0
    0 Comments

    假设您的内容在名为mystring的字符串内...您可以使用:

    @Html.Raw(mystring)
    

    或者您可以将您的字符串转换为HtmlString或其它实现IHtmlString的类型,并在模型或直接内联中使用常规的@

    @{ var myHtmlString = new HtmlString(mystring);}
    @myHtmlString
    

    0