2

你如何在 MVCContrib 网格中显示回车?我尝试将退货替换为"<br>",但这实际上<br>在我的显示器中显示 "test<br>test"

<div id="noteList">
    @Html.Grid(Model).Columns(column => {
        column.For(x => x.TimeStamp);
        column.For(x => x.UserName);
        column.For(x => x.Note.Replace("\r\n","\"<br>\"")).Named("Note");
        }).Attributes(Style => "text-aligh: center", @Class => "linkGrid")
</div>

有没有办法让浏览器呈现原始返回的“\r\n”?

4

1 回答 1

5

您可以使用自定义列:

column.Custom(item => @item.Note.Replace("\r\n", "<br/>")).Named("Note");

但更安全和恕我直言,更强大的解决方案是使用自定义 HTML 帮助器:

public static class HtmlExtensions
{
    public static IHtmlString FormatNote(this HtmlHelper html, string note)
    {
        if (string.IsNullOrEmpty(note))
        {
            return MvcHtmlString.Empty;
        }
        var lines = note.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
        return MvcHtmlString.Create(string.Join("<br/>", lines.Select(x => html.Encode(x))));
    }
}

进而:

column.Custom(item => Html.FormatNote(item.Note)).Named("Note");
于 2011-07-06T20:37:21.423 回答