我有一个保存到数据库的字符串。问题是,当我处于编辑模式表单时,它只显示换行符。当我在我的网页中显示它(没有编辑)或在电子邮件中使用时,字符串显示没有换行符,它们全部一起运行(见下图)。我正在使用MVC 4和VS2012。
有人可以告诉我如何让换行总是显示出来吗?
该字符串是从包含以下代码的表单生成的:
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.
换行符在编辑表单中工作: works in edit form http://www.leadingedgewebsites.com/linefeedworks.jpg
在网页上显示时,换行不起作用: Does Not Work on Webpage http://www.leadingedgewebsites.com/linefeednowork.jpg
换行不适用于电子邮件: Does Not Work in email http://www.leadingedgewebsites.com/inemail.jpg
感谢您抽出宝贵时间来帮助我。
谢谢。
答案 0 :(得分:1)
HTML忽略所有无关的空格,包括换行符。
要更改此设置,请使用<pre>
标记或white-space: pre
CSS属性。
答案 1 :(得分:0)
如果您想在网页上显示新行,请使用换行符代码 - <BR>
答案 2 :(得分:0)
对于网页,您必须将" \r\n "
替换为<br />
,因为它只能识别HTML换行符。
对于电子邮件和其他内容,您应使用Environment.NewLine
代替" \r\n "
,因为这将显示基于容器的换行符。