C# – Add sorting, filtering, and paging – ASP.NET MVC – Video

Sorting, filtering, paging and summary of an app are quite important features. In this video, which is a continuation of the Microsoft tutorial, I present step-by-step how to do each one of these.

It is quite a large video, but I hope it is worthy. Out of the sorting, filtering, paging and summary, I have decided to present the sorting capabilities of the app here. The sorting is done in the Index method of the StudentController.cs. Thus, in the app, we have clickable Last Name and Enrollment Date:

The code, presenting the business logic of the sorting and the filtering, as they are both in the Index method is here:

The idea is that we pass 4 parameters – sortOrder, currentFilter, searchString and pageNumber and the Index starts rendering the page correspondingly. If it is a “search” action and not a normal display action, it loads the first page, with the search results. If it is a search, it loads only the results on the search. Then it implements the order, if it is to be implemented. The rendering of the page in the Views>Students>Index.cshtml file is even more interesting:

There the filter is present at the beginning. The last part of the rendering are the arrows, which are used for Next and Previous. These are accessing the class PaginatedList.cs and using its corresponding properties to assign values of preDisabled and nextDisabled .

The PaginatedList.cs class and the whole code is available in GitHub here: https://github.com/Vitosh/ASP/tree/master/EFCoreAsp.NetMvcWebApp/ContosoUniversity003

Enjoy!

Tagged with: , , , ,