---
title: "How to sorting  with the entity framework is an ASP.NET MVC Application"  
description: "How to sorting  with the entity framework is an ASP.NET MVC Application"  
author: "Anupam Mishra"  
published: 2016-02-05  
updated: 2016-02-05  
canonical: https://www.mindstick.com/forum/33957/how-to-sorting-with-the-entity-framework-is-an-asp-dot-net-mvc-application  
category: "asp.net"  
tags: ["c#", "asp.net", "asp.net mvc"]  
reading_time: 2 minutes  

---

# How to sorting  with the entity framework is an ASP.NET MVC Application

Hello Everyone,I want to know how to sort record with the using of [entity](https://www.mindstick.com/forum/160562/database-connectivity-using-entity-framework-database-first-approach) [framework in ASP.NET](https://www.mindstick.com/forum/158854/what-is-the-purpose-of-the-razor-pages-framework-in-asp-dot-net-core) MVC. Can [anyone give me](https://answers.mindstick.com/qa/41194/can-anyone-give-me-some-high-and-low-points-of-each-of-the-four-first-presidential-administrations) a solution with example.\
Thank you.

## Replies

### Reply by Anupam Mishra

Finally, I have solve this problem.\
For solution of above problem, we are creating one student controller and its index view. We want to display a sort records(i.e. First Name,Last Name or Enrollment date) in index view.this is a code of Student Controller Index Action:

```
public ActionResult Index(string sortOrder)
{
   ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name" : "";
   ViewBag.DateSortParm = sortOrder == "Date" ? "Date_desc" : "Date";
   var students = from s in db.Student  // Student is a table i.e. containing records of student
                  select s;
   switch (sortOrder)
   {
      case "Name":
         students = students.OrderByDescending(s => s.FirstName);
         break;
      case "Date":
         students = students.OrderBy(s => s.EnrollmentDate);
         break;
      case "Date_desc":
         students = students.OrderByDescending(s => s.EnrollmentDate);
         break;
      default:
         students = students.OrderBy(s => s.FirstName);
         break;
   }
   return View(students.ToList());
}
```

In Views\Student\Index.cshtml, We have changed below lines:

```
<p>
    @Html.ActionLink("Create New Student Record", "Create")
</p>
<table>
    <tr>
        <th>
            @Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th> Last Name
        </th>
        <th>
            @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
```


---

Original Source: https://www.mindstick.com/forum/33957/how-to-sorting-with-the-entity-framework-is-an-asp-dot-net-mvc-application

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
