Sort in ASP.NET Core -Session 55

Sort List ASPNET Core MVC - Session55

During this session, we talk about Sort in ASP.NET Core and implementing this feature on the Web App that we have developed until this session for this tutorial. Generally, we can sort a list in Ascending or Descending format. Thus, in this session, we implement both formats to cover more detail about this topic. Also in this session, we use switch case structure to avoid using several If else structures.

Sort List in ASP.NET Core

To implement this feature on a Web App, we need to modify the View to enable the users to submit their requests for sorting. There are several ways to deploy these changes and in this tutorial, we use Query String. Therefore, we start this process by changing the respective View.

If you remember during the previous sessions, we develop a list to view the list of Costs (Cost/Index). Now we change the view and change the Cost and the Payment Method property to the anchor element. Then with asp-route Tag Helper passes the required Query String to the Controller.

As you can see above we use asp-route Tag Helper to pass the required data to the Controller. Also, the full code of the Cost/Index view is available below.

Next, we need to change ICostRepositoy and then the respective repository to perform the sort feature on the list, based on the client request.

As you can see, now the GetCostList method expects a new input element which is sortby and we can sort the list based on the sort by value.

As it has been shown above, the sort has been implemented on the list at the end of this method with OrderBy and OPrderByDescending methods. Also, we use a switch case structure to evaluate the sortby value.

Finally, we can change the Controller to receive the data from the View.

As it might be seen above, we receive the sortby value as an input parameter, then we send it to the repository and receive the sorted list to be passed to the View.

If you need more details, watch this session video. Also, to be updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE.

0.00 avg. rating (0% score) - 0 votes

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments