Consider the following example:
<div>@ViewBag.MyData</div>
What to do if ViewBag.MyData contains a string with "\n" (newlines) in it, that I want to change into "<br/>
"? When I include "<br/>
" instead of the newlines, it gets encoded in the browser, which is bad.
How can I help this?
use @Html.Raw(ViewBag.MyData.Replace("\n", "</br>"))
the following will do what you need...
@MvcHtmlString.Create(ViewBag.MyData.Replace("\n", "</br>"))
The Create
method will take a HTML string and render it as would be expected if you just typed the HTML straight onto the page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With