且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 13:27:28

这个解决方案对我有用,因为我有类似的问题。



在你的控制器中,保存笔记时你会做一些像



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.