Search code examples
c#htmlasp.net-mvctext-formatting

ASP.Net MVC - Displaying text with newlines in


I have some text, stored in a table. I'm using asp.net mvc to display this.

One of my views, has the following:

<%=(Model.QuestionBody.Replace("\r\n", "<br/>").Replace ("\r", "<br/>")) %>

Like this, it displays correctly However, if I omit the .Replace, it displays all on one line.

The same data displayed in a TextBox however, displays properly formatted.

My question is- Is there a better way of displaying the text in my View?


Solution

  • You can put your text in a "pre" tag.

    <pre>
       <%= Model.QuestionBody %>
    </pre>
    

    However usually this kind of text is stored in the database as html including the
    tags.