在MVC剃刀中的新行textarea中显示文本 [英] Display text in new line textarea in MVC razor
问题描述
我的mvc应用程序中有一个textarea当用户保存它时例如
hi
怎么样
你
但是当用户想要编辑那个textarea时它会显示你好,你好吗
但是我想以这种方式/>
嗨
怎么样
你
我尝试过:
@ Html.TextAreaFor(model => model.Notes,new {@class =form-control,placeholder =Name, required =required,@rows = 5})
i have a textarea in my mvc application when the user save it for example
hi
how are
you
but when user want to edit that textarea it shows hi how are you
but i want in that way
hi
how are
you
What I have tried:
@Html.TextAreaFor(model => model.Notes, new { @class = "form-control", placeholder = "Name", required = "required", @rows = 5 })
推荐答案
这个解决方案对我有用,因为我有类似的问题。
在你的控制器中,保存笔记时你会做一些像
This solution worked for me as I had similar issue.
In your controller, when saving the note you would do something like
entity.Note = HttpUtility.HtmlEncode(model.Note.Replace(Environment.NewLine, "<br>"));
然后在您的视图中显示时,您可以执行类似
的操作
Then when displaying in your view you could do something like
<textarea name="Note"><br mode="hold">@(new HtmlString(HttpUtility.HtmlDecode((note.Note1))))<br mode="hold"></textarea>
这应该有效,我没有编译这个/拉这来自现有代码,因此您可能需要调整它以满足您的需求。但它应该引导你朝着正确的方向发展。
That should work, I did not compile this/pull this from existing code so you may have to tweak it to meet your needs. But it should hopefully steer you in the right direction.
这篇关于在MVC剃刀中的新行textarea中显示文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!