在MVC剃刀中的新行textarea中显示文本

时间:2016-09-29 09:01:11

标签: asp.net-mvc

我的mvc应用程序中有一个textarea,当用户保存它时,例如

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 })

1 个答案:

答案 0 :(得分:0)

用新的换行符替换每个空格

修改ActionResult,以便在保存时,将每个" "替换为Environment.NewLine

像这样:

NoteManager.Save(note.Replace(" ", Environment.NewLine));

修改

用换行符替换第一个和最后一个空格:

// 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