2016-09-29 185 views
-2

我有一個textarea在我的MVC應用程序,當用戶保存例如顯示文本在MVC剃刀

hi 
how are 
you 

但是當用戶希望編輯的textarea它顯示

hi how are you 

但我想以這種方式

hi 
how are 
you 

我已經試過

@Html.TextAreaFor(model => model.Notes, new { @class = "form-control", placeholder = "Name", required = "required", @rows = 5 }) 
+0

不清楚你在問什麼。如果在'textarea'中有3行文本並正確保存,則將其讀回textarea,它將顯示爲3行文本 –

+0

您不能通過textarea保存換行符。因爲你需要使用editorfor剃刀 –

+0

我將使用你的代碼我不清楚你的解決方案 –

回答

0

要使用新行字符

編輯ActionResult替換每個空格,這樣,當您保存,你替換每個" "Environment.NewLine

像這樣:

​​

編輯

要替換新行Ë1日和最後空間:

// set string 
string s = "hi how are you"; 

// make sure that there are no trailing spaces otherwise our code will not work 
s = s.TrimEnd(' '); 

// remove 1st space 
// get index 
int index = s.IndexOf(" "); 
// remove space 
s = s.Remove(index, 1); 
// replace with newline 
s = s.Insert(index, Environment.NewLine); 

// remove last space 
// get index 
index = s.LastIndexOf(" "); 
// remove space 
s = s.Remove(index, 1); 
// replace with newline 
s = s.Insert(index, Environment.NewLine); 

Console.Write(s); 

小提琴:https://dotnetfiddle.net/kg7kcf

+0

這會給4行文本! (如果OP已經正確保存了值,則不需要做任何事情) –

+0

@StephenMuecke - 現在他編輯了這個問題。我花了一段時間才明白問題所在。他對我來說並不容易......我從一開始就有了完全不同的答案。他要求的默認值 – Ted

+0

特德我認爲你現在明白了,請給我解決方案 –