Show text line breaks in ASP.NET MVC Razor view

If you have a string with line breaks and want to show that text with the line-breaks intact in your Razor-based view, you can do this without replacing all \r\n with <br/>-tags. Instead present the text in an element that has the style property white-space set to pre-line. You should really add a class like:

<style>
.line-breaks { white-space:pre-line; }
</style>

<span class="line-breaks">@Model.MyText</span>

Related posts:

Comments

comments powered by Disqus