【问题标题】:Line breaks are not rendered in MVC Razor View在 MVC Razor 视图中不呈现换行符
【发布时间】:2013-11-04 00:50:53
【问题描述】:

我在控制器中生成了以下字符串值。

 cc.lstchat = "Reply the Number with your question... <br />";

                foreach (clsChat item in lstchat)
                {

                    cc.lstchat =cc.lstchat + item.DisplayNumber+". " + item.ChatItem+" <br/> ";
                }

现在我试图在我的视图中的 div 标记内显示上述字符串值,但它不会呈现换行符。

<div id="divChat" style="width:400px;height:300px;border:double">
           @Model.lstchat.ToString()
       </div>

【问题讨论】:

标签: html asp.net asp.net-mvc razor


【解决方案1】:

请注意@Html.Raw(),因为 HTML 注入(例如,我的评论将是 &lt;script&gt;...&lt;/script&gt; test - 这是一个 XSS。如果你只想换行 - 考虑 this answer

@Html.Raw(Html.Encode(Model.CommentText).Replace("\n", "<br />"))

【讨论】:

    【解决方案2】:

    使用Html.Raw()它用于将任何字符串呈现为HTML。

    `@Html.Raw("input data in here is rendered as HTML only")`
    

    【讨论】:

      【解决方案3】:

      试试@Html.Raw方法

      @Html.Raw(Model.lstchat)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-05-06
        • 2011-05-12
        • 2012-03-03
        • 1970-01-01
        • 2018-08-11
        • 2015-08-02
        相关资源
        最近更新 更多