简体   繁体   中英

How to properly display Line Feed in string when used on webpage or email

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.

HTML ignores all extraneous whitespace, including newlines.

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

如果要在网页上显示新行,请使用换行标记- <BR>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM