2016-09-29 188 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

特德我认为你现在明白了,请给我解决方案 –