Displaying HTML String from Model in MVC Razor View

前端 未结 4 437
星月不相逢
星月不相逢 2021-01-12 01:43

I have a Model filed that returns an HTML string with line break BR tag, but How do I display that HTML on the browser ? The problem ins instead putting the line break, the

相关标签:
4条回答
  • 2021-01-12 02:18

    Try this :

    @(new HtmlString(stringWithMarkup))
    

    and you can create a HTML helper too!:

    @helper RawText(string s) {
        @(new HtmlString(s))
    }
    
    0 讨论(0)
  • 2021-01-12 02:18

    You can just omit both DisplayFor and modelItem =>, so:

    @Html.Raw(item.Speaking)
    
    0 讨论(0)
  • 2021-01-12 02:21

    In MVC4

    Instead of using @Html.Raw(modelItem => item.Speaking)

    You can use @Html.Raw(@Model.Speaking.ToString())

    It works for me and I hope this help someone else too

    0 讨论(0)
  • 2021-01-12 02:21

    MVC 5

    @Html.Raw(WebUtility.HtmlDecode(item.Speaking))
    
    0 讨论(0)
提交回复
热议问题