Justin M.
Justin M.

Reputation: 21

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?

Upvotes: 0

Views: 12812

Answers (2)

JRS
JRS

Reputation: 773

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

Upvotes: 1

Jéf Bueno
Jéf Bueno

Reputation: 434

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

Example:

part1 + "<br>" + part2;

Upvotes: 0

Related Questions