开发者

MVCContrib Grid - Show carriage returns

How do you show carriage returns inside a MVCContrib Grid? I've tried replacing the returns with "<br>", however that is actually showing a <br> in my 开发者_Go百科display "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>

Is there a way to get the browser to render the original return's "\r\n"?


You could use a custom column:

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

But a safer and IMHO a more robust solution would be to use a custom HTML helper:

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))));
    }
}

and then:

column.Custom(item => Html.FormatNote(item.Note)).Named("Note");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜