0

Output from controller below is rendered without line breaks in IE9 How to force browser to render text in separate lines as created ?

ASP.NET / Mono MVC2 is used in .NET 3.5

    public ActionResult Index()
    {
        var sb = new StringBuilder();
        sb.AppendLine("Line1");
        sb.AppendLine("Line2");
        return new ContentResult() { Content = sb.ToString() };
    }
Andrus
  • 26,339
  • 60
  • 204
  • 378

3 Answers3

2

It depends on what you trying to do. Browser ignore simple new line, for C# it is \r\n. Use <br /> (html tag for new line) or try <pre> tag:

<pre>
   Line 1
   Line 2
</pre>

Demo: http://jsfiddle.net/CQeTX/

webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • Thank you. Some lines are long and IE9 shows only start of them. How to force lines to wrap to next line so that they are fully visible in pre which is placed to div browser window ? – Andrus Mar 02 '13 at 17:53
  • @Andrus Try this solution: http://stackoverflow.com/questions/248011/how-do-i-wrap-text-in-a-pre-tag – webdeveloper Mar 02 '13 at 21:26
1

When you use AppendLine, StringBuilder appends newlines. If you want to show line breaks in the browser, you have to add <br/>:

sb.AppendLine("Line1<br/>");
sb.AppendLine("Line2<br/>");

or

sb.Append("Line1<br/>");
sb.Append("Line2<br/>");
chue x
  • 18,573
  • 7
  • 56
  • 70
0

If it is part of the html on the page

    sb.AppendLine("Line1<br />");

or if it is in the code

    sb.AppendLine("Line1\n");
Rob
  • 357
  • 1
  • 3
  • 13