我需要用
替换environment.Newline html标签
我正在建立一个电子商务网站。当用户将意大利面的列表复制到“描述”文本区域时,我需要能够在
标记内以这种方式显示它。现在一切都混杂在一起了。我尝试使用替换方法将换行符替换为
,但它显示
就好像它是字符串的一部分。我确信这并不难,我只是很难找到答案。
这是我认为的代码:
<%: Model.Description.Replace(Environment.NewLine, "
") %>
在我的控制器中,我只是将编码的字符串保存在数据库中,然后将其取出并按原样传递给视图。
I am building an ecommerce site. When the user copy pasta's a list into a "description" text area I need to be able to display it that way inside a <p>
tag. Right now it is all jumbled together. I have tried the replace method to replace the newline with a <br/>
, but it displays the <br/>
as if its part of the string. I'm sure it's not hard, I'm just having a hard time finding my answer.
Here is the code in my view:
<p id="description"><%: Model.Description.Replace(Environment.NewLine, "<br/>") %></p>
In my controller I just save the encoded string in my database and then grab it out and pass it as is to the view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用正则表达式。如果你使用 javascript 执行此操作,你可以尝试这个,
这意味着,找到字符串中换行符的所有实例,并将其替换为 html 换行符。
You need to use a regular expression. If you are doing this with javascript you can try this
That means, find all instances of the new line character in the string and replace it with a html line break.
显示为文本,因为控件的内容会自动进行 html 编码。使用
Literal
控件显示列表,html 标签将不会被编码,并且
将显示为换行符。但请记住,显示来自用户的未编码数据可能很危险。 (脚本攻击)
The
<br/>
is shown as text because the contents of your control get html encoded automatically.Use an
Literal
control to display the list, the html-tags will then not be encoded and<br/>
will be shown as a line break.Keep in mind though, that showing unencoded data coming from a user can be dangerous. (scripting attacks)