从显示文本区在ASP.NET MVC中的新行 [英] Show new lines from text area in ASP.NET MVC
问题描述
我目前正在创建使用ASP.NET MVC应用程序。我有一个textarea里面的一些用户输入和我想要显示该文本以< BR /> S,而不是换行。在PHP中有一个名为nl2br功能,这正是这一点。我搜索了在ASP.NET/C#等值网络,但没有找到适合我的解决方案。
拳头一个是这个(没有做我什么,评论只是没有印新线):
<%
字符串评论= Html.En code(Model.Comment);
comment.Replace(\\ r \\ n,< BR /> \\ r \\ n);
%GT;
<%=评论%GT;
第二个我发现了这个(Visual Studio中告诉我VbCrLf没有在这种情况下可用 - 我试图在视图和控制器):
<%
字符串评论= Html.En code(Model.Comment);
comment.Replace(VbCrLf,&所述峰; br />中);
%GT;
<%=评论%GT;
尝试(未测试自己):
注释= comment.Replace(System.Environment.NewLine,< BR />中);
更新:
刚刚测试过的code - 它的作品在我的机器上
更新:
另一种解决方案:
System.Text.StringBuilder SB =新System.Text.StringBuilder();
System.IO.StringReader SR =新System.IO.StringReader(originalString);
字符串TMPS = NULL;
做{
TMPS = sr.ReadLine();
如果(TMPS!= NULL){
sb.Append(TMPS);
sb.Append(< BR />中);
}
}而(TMPS!= NULL);
变种convertedString = sb.ToString();
I'm currently creating an application using ASP.NET MVC. I got some user input inside a textarea and I want to show this text with <br />s instead of newlines. In PHP there's a function called nl2br, that does exactly this. I searched the web for equivalents in ASP.NET/C#, but didn't find a solution that works for me.
The fist one is this (doesn't do anything for me, comments are just printed without new lines):
<%
string comment = Html.Encode(Model.Comment);
comment.Replace("\r\n", "<br />\r\n");
%>
<%= comment %>
The second one I found was this (Visual Studio tells me VbCrLf is not available in this context - I tried it in Views and Controllers):
<%
string comment = Html.Encode(Model.Comment);
comment.Replace(VbCrLf, "<br />");
%>
<%= comment %>
Try (not tested myself):
comment = comment.Replace(System.Environment.NewLine, "<br />");
UPDATED:
Just tested the code - it works on my machine
UPDATED:
Another solution:
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringReader sr = new System.IO.StringReader(originalString);
string tmpS = null;
do {
tmpS = sr.ReadLine();
if (tmpS != null) {
sb.Append(tmpS);
sb.Append("<br />");
}
} while (tmpS != null);
var convertedString = sb.ToString();
这篇关于从显示文本区在ASP.NET MVC中的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!