locked
Displaying a sentence in new line in the view for dynamic content from database RRS feed

  • Question

  • User1255309776 posted

    Hi guys,

    In our CRUD operations, when writing description of the post to create one, sometimes we need to write some sentences in new line.  Even if we do it when writing in textarea, after post creation, in the database all sentences are in one row in the sequence, thus, when we call the description property to the relevant view to display, it displays all sentences one after one (in consecutive way).

    What's the solution of displaying a sentence in a new line in the view for dynamic content from database?

    thanks in advance!

    Tuesday, May 21, 2019 7:43 PM

Answers

  • User475983607 posted
    Use the @Html.Raw() helper to display unencoded content.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 26, 2019 2:36 PM

All replies

  • User475983607 posted

    Browsers do not display white space characters like line breaks.  Convert the line breaks to the HTML <br /> tag.

    string Content = "This is \r\n a test";
    Content = Content.Replace(System.Environment.NewLine, "<br />");

    You'll need to use the Htmlhelper.Raw to render the HTML unencoded.

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/razor?view=aspnetcore-2.2#expression-encoding

    Tuesday, May 21, 2019 8:00 PM
  • User-854763662 posted

    Hi  FaridGN ,

    Try to use the CSS white-space property in the place you want to display  , the following setting will show the line break format you used when adding content

    <span style="white-space: pre-line">@Model.CommentText</span>

    Note : pre-line: Sequences of white space are collapsed. Lines are broken at newline characters, at <br>, and as necessary to fill line boxes.

    Best Regards ,

    Sherry

    Wednesday, May 22, 2019 3:25 AM
  • User-2054057000 posted

    Hi guys,

    In our CRUD operations, when writing description of the post to create one, sometimes we need to write some sentences in new line.  Even if we do it when writing in textarea, after post creation, in the database all sentences are in one row in the sequence, thus, when we call the description property to the relevant view to display, it displays all sentences one after one (in consecutive way).

    What's the solution of displaying a sentence in a new line in the view for dynamic content from database?

    thanks in advance!

    You can also use CKEditor in ASP.NET MVC which automatically displays sentence in newline. 

    Your text area can be easily converted into text editor with CKEditor, and it provides you with full editing power. It is same as the message box in this forum site.

    Wednesday, May 22, 2019 4:57 AM
  • User1255309776 posted

    Thanks for answer, but I didn't get completely.

    First, where I'll write the code you mentioned above, in the action of controller which creates the post?

    And will it generate <br/> on every "enter" I press while writing description in the input (textarea)?

    Note that my issue concerns dynamic content and the result will come from database to the view.

    Wednesday, May 22, 2019 5:17 PM
  • User1255309776 posted

    I don't think this way would solve my problem. Note that I have shared view with standard code for post inside foreach loop, and some posts may contain special new line sentences in the description which I'll set manually in the input, but some not, they'll just be written like sample paragraph which will automatically create new line because of its limited box frame as usual.

    Wednesday, May 22, 2019 5:23 PM
  • User475983607 posted

    The solution is very simple.  The browser does not render white space characters.  You must use HTML to format content.

    First, where I'll write the code you mentioned above, in the action of controller which creates the post?

    It depends on what you want.  Either save the HTML in the database or replace the line feeds when fetching the data.

    And will it generate <br/> on every "enter" I press while writing description in the input (textarea)?

    If you are referring to the code above, yes.

    Wednesday, May 22, 2019 5:37 PM
  • User1255309776 posted

    Thanks for reply,

    I added your code to the action which returns the text to the detailed view page. Though it generated <br/> on every enter I typed, in the view it shows that tag between text like this:

    Here is sample:<br/>first one<br/>second one<br/>third one

    What do I need to write in view to make it recognize and differ html tag from string text?

    Note that this is dynamic text, so I called @Model.Detailed.Description property in the view which represents the input text.

    thanks in advance

    Sunday, May 26, 2019 2:07 PM
  • User475983607 posted
    Use the @Html.Raw() helper to display unencoded content.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, May 26, 2019 2:36 PM