Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: How do I display multiline text?

View Model:

public class Note
{
    [DataType(DataType.MultilineText)]
    public string Text { get; set; }
}

Default editor template renders a <textarea> element with the newlines preserved.

The default display template renders the text as a single string with the newlines removed.

I tried this, but it doesn't work:

~/Views/Shared/EditorTemplates/MultilineText.cshtml

@model string

@Html.Raw(Model.Replace(System.Environment.NewLine, "<br />"))

I can do something silly like @Html.Raw(Model.Replace("e", "<br />")) and it will work but of course I only want to replace the newline characters the <br /> element! I also tried using @"\n" and that didn't work either.

Any ideas?

Thanks!

like image 641
Jeff Camera Avatar asked Sep 23 '11 00:09

Jeff Camera


2 Answers

You could try this:

@Html.Raw("<pre>"+ Html.Encode(Model) + "</pre>");

This will preserve your content and show it as-is.

like image 133
scartag Avatar answered Sep 20 '22 14:09

scartag


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

like image 33
Valamas Avatar answered Sep 19 '22 14:09

Valamas