0

I am making a webpage... the controller is written in C# and the webpage is in cshtml. In the c# file, I am constructing strings to be put into the table on the html page. When I have my strings, I need line breaks within them, so I have things like stringpart1 + "\n" + stringpart2 in hope of the webpage displaying the string as two separate lines as I fill my table cells. However, everything seems to print out on the same line. I have also tried "\r\n" and System.Environment.NewLine instead. Any ideas as to a potential fix?

Justin M.
  • 21
  • 1
  • 1
  • 5

2 Answers2

1

Here is a fix which worked for me. In your .cshtml, use this: @Html.Raw(System.Web.HttpUtility.HtmlEncode([your content containing \n]))

JRS
  • 569
  • 9
  • 26
  • 1
    That works. However, if you fill it with content from a variable that can be set by a user, than you have a potential XSS. So only use it for fixed, known data. – JHBonarius Sep 11 '20 at 06:55
0

Line breaks in HTML are represented by <br> tag. Change \n and/or \r to <br>.

Example:

part1 + "<br>" + part2;
Jéf Bueno
  • 425
  • 1
  • 5
  • 23