2013-10-11 76 views
0

我有一個字符串,我將其保存到數據庫。問題是,當我處於編輯模式表單時,它只顯示換行符。當我在我的網頁上顯示它(不編輯)或在電子郵件中使用它時,字符串顯示時沒有換行符,它們都一起運行(請參閱下面的圖片)。我使用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

我很感激你花時間幫助我。

謝謝。

+1

很遺憾,您必須根據目標格式將它們替換爲正確的_thing_。例如HTML(頁面和電子郵件)的
或RTF電子郵件的\ par(純文本電子郵件的\ n)。 –

+0

使用'
'而不是'\ r \ n' –

+1

您有一個XSS洞。 – SLaks

回答

0

的網頁你有你的" \r\n "<br />來代替,因爲它只能識別HTML換行符。

對於電子郵件和其他您應該使用Environment.NewLine而不是" \r\n ",因爲這將顯示基於容器的換行符。

+1

完美運作。將字符串導出到Webform或電子郵件時,我使用ViewBag.Comment = strComment.Replace(「\ r \ n」,「
」),然後在我的視圖中使用@ Html.Raw(Server.HtmlDecode(@ ViewBag.Comments)。工作得很好,非常感謝你的幫助。 – user2789697

0

如果你想顯示在網頁上使用換行符標籤的新生產線 - <BR>

1

HTML忽略所有無關的空格,包括換行符。

要更改此設置,請使用<pre>標記或white-space: pre CSS屬性。

相關問題