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?
Asked
Active
Viewed 1.1k times
2 Answers
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
-
1That 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
-
3that was the first thing I tried it just adds
literally to the string – Justin M. Jul 19 '17 at 16:53
" + stringpart2 – dnickless Jul 19 '17 at 16:49