0

I have a string that I save to a database. Problem is it only displays the line feed when I am in the edit mode form. When I just display it (no editing) in my webpage or use it in an email the string displays with no line feeds and it is all run together (See pictures below). I'm using MVC 4 and VS2012.

Can anyone please tell me how to get the line feeds to always display?

The string is generated from a form with the following code:

string LF = " \r\n ";
//string LF = Environment.NewLine; // tried this also, did'nt work.
string appendComments = "  ";

                contact.Subject = "Quick Quote";
                appendComments += "Make Sure to Contact Customer by: " + Request.Form["radio"].ToString(); ; 
                appendComments += LF + LF + "CARPET CLEANING: " + LF;
                if ((Request.Form["bedRms"]) != "0") { appendComments += "Bedrooms =" + Request.Form["bedRms"] + ", " + LF; }
                if ((Request.Form["familyRm"]) != "false" || (Request.Form["familyRm"]) == "on" ) { appendComments += "Family Room = Yes, " + LF; }
                if ((Request.Form["livingRm"]) != "false" || (Request.Form["livingRm"]) == "on") { appendComments += "Living Room = Yes, " + LF; }
                if ((Request.Form["diningRm"]) != "false" || (Request.Form["diningRm"]) == "on") { appendComments += "Dining Room = Yes, " + LF; }
                if ((Request.Form["ld-combo"]) != "false" || (Request.Form["ld-combo"]) == "on") { appendComments += "Living/Dining Combo = Yes, " + LF; }
                if ((Request.Form["pets"]) != "false" || (Request.Form["pets"]) == "on") { appendComments += "Pet Spot/Stain Issue = Yes " + LF; }
Here I save it to the database.

Line Feed Works in the Edit Form: works in edit form http://www.leadingedgewebsites.com/linefeedworks.jpg

Line Feed Does Not Work When Displaying it on the webpage: Does Not Work on Webpage http://www.leadingedgewebsites.com/linefeednowork.jpg

Line Feed Does Not Work on Email: Does Not Work in email http://www.leadingedgewebsites.com/inemail.jpg

I do appreciate you taking the time to help me.

Thank you.

user2789697
  • 227
  • 1
  • 7
  • 13

3 Answers3

1

HTML ignores all extraneous whitespace, including newlines.

To change this, use the <pre> tag, or the white-space: pre CSS property.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

If you want to display a new line on a webpage use line break tag - <BR>

Jaycee
  • 3,098
  • 22
  • 31
0

For web pages you have to replace your " \r\n " with <br />, since it only recognizes HTML line breaks.

For Email and other you should use Environment.NewLine instead of " \r\n ", since that will display a line feed based on the container.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • 1
    That worked perfectly. When exporting the string to the webform or email I used ViewBag.Comment = strComment.Replace("\r\n", "
    ") and then @Html.Raw(Server.HtmlDecode(@ViewBag.Comments) in my view. Worked perfectly. Thank you so much for your help.
    – user2789697 Oct 12 '13 at 00:57