How to display encoded HTML as decoded in MVC 3 Razor?

后端 未结 7 1501
伪装坚强ぢ
伪装坚强ぢ 2021-02-06 21:54

I\'m using Razor in MVC 3 and Asp.net C#.

I have a View with the following code. model.ContentBody has some HTML tags.

I would need display this HTM

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-06 22:35

    @Html.Raw was not work for me here is example:-

      string name = "<p><span style="font-size: small; color: #ff0000;"><span style="font-size: small;">&nbsp;<span style="font-size: large; color: #000000;">Hi</span><br />&nbsp; <br />This is just a sample,<br />This will not work with @Html.Raw(),<br />";
      @Html.Raw(name);
    

    But this worked instead:-

    @MvcHtmlString.Create(HttpUtility.HtmlDecode(@model.ContentBody))
    

    or you can also use :-

    @Html.Raw(HttpUtility.HtmlDecode(@model.ContentBody));
    

提交回复
热议问题