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 dis开发者_Python百科plays 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.
You need to use a regular expression. If you are doing this with javascript you can try this
value = value.replace(/\n/g, '<br />');
That means, find all instances of the new line character in the string and replace it with a html line break.
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)
精彩评论