I'm using the following to make the text output the line breaks entered in a <textarea>
HTML element.
MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))
Is there a nicer way to do this?
I'm using the following to make the text output the line breaks entered in a <textarea>
HTML element.
MvcHtmlString.Create(Model.Post.Description.Replace(Environment.NewLine, "<br />"))
Is there a nicer way to do this?
Maybe you can output the text inside a <pre> tag.
It's working for me.
string Model.Text having < br/> tag inside.
Your code is vulnerable to XSS attacks as it doesn't HTML encode the text. I would recommend you the following:
and then in your view you can safely:
Just use a tag.
<pre>@Model.Post.Description</pre>
Or
There's an even better/awesome solution that employs CSS white-space property:
Using this you avoid Cross-site scripting (XSS) vulnerabilities...
Works like a charm with ASP.NET MVC Razor engine.
Here is my solution.
and of course, you will have to add following using statement for Regex to work.
Hope it is useful for someone.