'New line character from c# to cshtml
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?
Solution 1:[1]
Line breaks in HTML are represented by <br>
tag. Change \n
and/or \r
to <br>
.
Example:
part1 + "<br>" + part2;
Solution 2:[2]
Here is a fix which worked for me. In your .cshtml, use this:
@Html.Raw(System.Web.HttpUtility.HtmlEncode([your content containing \n]))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Jéf Bueno |
Solution 2 | JRS |