How to list posts from newest to oldest in asp net mvc5


ASP.NET is a popular programming language used for building web applications. One common in web development is to display a list of posts or articles in a specific order, such as from newest to oldest. In this article, we will explore how to achieve this in ASP.NET MVC5.


When it comes to sorting posts, we need to consider the date of each post. In ASP.NET MVC5, we can use LINQ (Language Integrated Query) to sort the posts based on creation date.

First, let's assume we have a model called “Post” with properties like “Title”, “Content”, and “CreationDate”. We can retrieve a list of posts from the using Entity Framework or any other data access technology.

public  Post
    public int Id { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }
    public DateTime CreationDate { get; set; }

Once we have the list of posts, we can sort them using LINQ's “OrderByDescending” method. This method allows us to sort the posts in descending order based on the “CreationDate” .

List posts = GetPostsFromDatabase();
List sortedPosts = posts.OrderByDescending(p => p.CreationDate).ToList();

Now, the “sortedPosts” list will contain the posts sorted from newest to oldest based on their creation date.

Sorted Posts

Once we have the sorted list of posts, we can display them in our ASP.NET MVC5 view. We can over the “sortedPosts” list and render each post using HTML .

Here's an example of how we can display the sorted posts in a view:

@model List

@foreach (var post in Model)





In the above example, we assume that the view's model is a list of posts. We iterate over each post and render its title, content, and creation date using HTML markup.


In this article, we have explored how to list posts from newest to oldest in ASP.NET MVC5. By using LINQ's “OrderByDescending” method, we can easily sort the posts based on their creation date. We have also seen how to display the sorted posts in an MVC view using HTML markup. With these techniques, you can now implement a sorted list of posts in your ASP.NET MVC5 .

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents