in Education by (1.9m 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.9m 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.

Related questions

0 votes
asked Feb 10 in Education by JackTerrance (1.9m points)
0 votes
asked Oct 30, 2020 in Technology by JackTerrance (1.9m points)
...