0 votes
in Education by (1.3m points)
I'm in need of some help in getting a View to layout data it is accessing from a database correctly.

I have a simple View on my MVC3 c# site to display the data stored in a database. The View code is below:

<div id="content" class="valign">

    <p>

        @foreach (var item in Model)

        {

                <i>@Html.DisplayFor(modelItem => item.News_Date)</i><br />        

                @Html.DisplayFor(modelItem => item.News_Entry)<br />

            <br />

        }

    </p>

</div>

This works fine initially, the data is displayed as shown below:

Date <br>

Post

Date <br>

Post

Date <br>

Post

...etc

The issue I have now is that when viewing the "Post" data using the method above it doesn't include carriage returns, so paragraphs etc are not displayed correctly with line breaks.

I received some help earlier from people on this site on how to achieve a bit of formatting when displaying typing data into a TextArea box and uploading it to a database, then displaying that data (including line breaks) on a separate page View. The method they gave me to display this data is below:

@foreach (var data in Model)

{                       

    @Html.Raw(data.News_Entry.Replace(Environment.NewLine, "<br/>"))

}

This preserved line-breaks that were used in the original data input.

However, now if I want to display data I have to use this method to display the "date":

@foreach (var item in Model)

{

    <i>@Html.DisplayFor(modelItem => item.News_Date)</i><br />

}

...and this method to display the "post":

@foreach (var data in Model)

{

    @Html.Raw(data.News_Entry.Replace(Environment.NewLine, "<br/>"))

}

...but it doesn't display the data on the page like before, i.e:

Date <br>

Post

It displays it as:

Date <br>

Date <br>

Date <br>

 <br>

Post <br>

Post <br>

Post <br>

Would anyone please be able to help me get around this?

Preferably, I'd like to have the Date displayed first, and the post displayed underneath it, whilst keeping linebreaks intact.

To maybe illustrate a bit better, I have a couple of screenshots:

Correct way to display

Incorrect way to display

Many thanks.

JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by (1.3m points)
Why not simply use HTML pre tag?

The pre tag defines preformatted text.

Text in a pre element is displayed in a fixed-width font (usually Courier), and it preserves both spaces and line breaks.

Learn More with Blogmepost

blogmepost Online Test

NCERT Questions & Answers

  • NCERT Class 12 Maths Solutions
  • NCERT Class 12 Physics Solutions
  • NCERT Class 12 Chemistry Solutions
  • NCERT Class 12 Biology Solutions
  • NCERT Class 10 Science Solutions
  • NCERT Class 10 Maths Solutions
blogmepost Q&A

CBSE Questions & Answers Portal

  • CBSE Class 12 Maths Questions Answers
  • CBSE Class 12 Physics Questions Answers
  • CBSE Class 12 Chemistry Questions Answers
  • CBSE Class 12 Biology Questions Answers
  • CBSE Class 10 Science Questions Answers
  • CBSE Class 10 Maths Questions Answers
Blogmepost

Learn Math Formula, Revise Notes

  • JEE Crash Course Questions & Answers
  • NCERT Course Questions and Answers
  • CBSE Board Questions Bank
  • ICSE Board Questions Bank
  • Free Study Materials
  • Question & Answers Bank for All Classes
  • Text Book Solutions
  • Previous Year Question Paper
  • NCERT Book Solutions

  • NCERT
  • NCERT Solutions
  • NCERT Solutions for Class 12 Maths
  • NCERT Solutions for Class 12 Physics
  • NCERT Solutions for Class 12 Chemistry
  • NCERT Solutions for Class 12 Biology
  • NCERT Solutions for Class 11 Maths
  • NCERT Solutions for Class 11 Physics
  • NCERT Solutions for Class 11 Chemistry
  • NCERT Solutions for Class 11 Biology
  • NCERT Solutions for Class 10 Maths
  • NCERT Solutions for Class 10 Science
  • NCERT Solutions for Class 9 Maths
  • NCERT Solutions for Class 9 Science
  • CBSE Classes

  • CBSE Class 5 Questions
  • CBSE Class 6 Questions
  • CBSE Class 7 Questions
  • CBSE Class 8 Questions
  • CBSE Class 9 Questions
  • CBSE Class 10 Questions
  • CBSE Class 10 Maths Questions
  • CBSE Class 11 Commerce Questions
  • CBSE Class 11 Engineering Questions
  • CBSE Class 11 Medical Questions
  • CBSE Class 12 Commerce Questions
  • CBSE Class 12 Maths Questions
  • CBSE Class 12 Engineering Questions
  • CBSE Class 12 Medical Questions
  • BOARDS

  • CBSE
  • ICSE
  • NCERT
  • IGCSE
  • Andhra Pradesh
  • Bihar
  • Gujarat
  • Jharkhand
  • Karnataka
  • Kerala
  • Madhya Pradesh
  • Maharashtra
  • Punjab
  • Rajasthan
...