如何确保添加到列表变量的最新条目被放置在顶部而不是在html中显示的底部?

时间:2021-05-08 04:16:43

I am using MVC + EF

我正在使用MVC + EF

I have a Feed xml file url that gets updated every 7 minute with items, every time a new item gets added I retrieve all the items to a list variable and then I add these varible to my database table. After that I fill a new list variable which is my ViewModel from the database table. Then I declare the ViewModel inside my view which is a .cshtml file and loop throught all of the objects and display them.

我有一个Feed xml文件url,每7分钟更新一次条目,每次添加一个新条目,我将所有条目检索到一个列表变量中,然后将这些变量添加到数据库表中。之后,我将从数据库表中填充一个新的列表变量,它是我的ViewModel。然后我在视图中声明ViewModel,它是一个.cshtml文件,循环遍历所有对象并显示它们。

How can I make sure that the newest items get placed on the top and not in the bottom?

我怎样才能确保最新的产品被放在顶部而不是底部呢?

This is how I display the items inside my cshtml note that I use a ++number so the newest item needs to be 1 and so on.. :

这是我在cshtml中显示项目的方式,我使用一个++号,所以最新的项目需要是1等等。:

@model Project.Viewmodel.ItemViewModel
 @{
     int number = 0;
     }
 }
 <div id="news-container">
 @foreach (var item in Model.NewsList)
            {
     <div class="grid">
     <div class="number">
                    <p class="number-data">@(++number)</p>
                </div>
                <p class="news-title">@(item.Title)</p>
                <div class="item-content">            
             <div class="imgholder">
            <img src="@item.Imageurl" />
            <p class="news-description">@(item.Description) <br />@(item.PubDate) | <a href="@item.link">Source</a></p>

        </div>
        </div>
      </div>                  
     }
  </div>

This is how I fill the viewmodel which I use inside the .cshtml file to iterate throught and display the items

这就是我如何填充viewmodel的方法,我在.cshtml文件中使用它来遍历和显示条目

    private void FillProductToModel(ItemViewModel model, News news)
    {
        var productViewModel = new NewsViewModel
        {

            Description = news.Description,
            NewsId = news.Id,
            Title = news.Title,
            link = news.Link,
            Imageurl = news.Image,
            PubDate = news.Date,
        };
        model.NewsList.Add(productViewModel);
    }

如何确保添加到列表变量的最新条目被放置在顶部而不是在html中显示的底部?

Sorry for the paint lol :P

抱歉,油漆lol:P。

Any kind of help is appreciated

任何形式的帮助都是值得感激的

1 个解决方案

#1


2  

@foreach (var item in Model.NewsList.OrderByDescending(n => n.PubDate)

#1


2  

@foreach (var item in Model.NewsList.OrderByDescending(n => n.PubDate)