简体   繁体   中英

How to display SQL Server CE data correctly in a View C# MVC3 Razor

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, ie:

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.

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.

Appologies for wasting the boards time - I managed to sort it out myself by adding the News_Date like below:

<p>
    @foreach (var data in Model)
    {
        <i>@Html.DisplayFor(modelItem => data.News_Date)</i><br />
        @Html.Raw(data.News_Entry.Replace(Environment.NewLine, "<br/>"))<br />
    }
</p>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM