Paging, Sorting, and Searching in ASP.NET MVC using PagedList and Dynamic Linq

PagedList.mvc is a lightweight API for server side paging in ASP.NET MVC. The only thing we want to avoid is reading all the records from the database and let the PagedList display the records by page. Obviously it's a bad design. What we want to achieved is to read and display the records per page.

In this article, we are going to explore how to read and display records using PagedList. Sort the records and provide search capability as well. We will also see how to use Dynamic Linq to avoid several if branching;

First: Start a ASP.NET MVC project. Go Managed Nuget Packages and install the following:
  • PagedList.mvc
  • Dynamic Linq
We are going to use Books' Record as our example.

Download Complete Source Code


The Screen Shot

The Project Structure


The Model

Book.cs The DbContext

BooksContext.cs We will use EF's Code First approach to build our database.

WhereExtension Class

WhereExtension.cs
I have included an extension on the Where Linq method to avoid too much branching. The purpose of this extension to perform Where operation when a condition is true.

Using the WhereIf method is very powerful, It will only execute the where query if the searchString variable is not null.

The Controller

HomeController.cs
Looking at the codes, we executed the Linq query at once; integrating the sort, search, and paging. This is to allow SQL statement to perform the TOP, ORDER, and  WHERE row_number at once.

Dynamic Linq was used for ordering purpose. Instead of using switch statement, we will directory use the field instead of the strongly type Linq Expression. Just make sure that the sortOrder variable matches the field/property that you are ordering.

The Index View
The @Html.PagedListPager will pass the sort order, search text and page number to the controller whenever the user interacts with these features.

Hope this article helps! Your thoughts!

21 comments

What nice example you are given

Reply

This is very good information for knowing the dotnet basic knowledage, Most of the institutes are providing asp.net with mvc online training institute in Ameerpet.

Reply

I am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read.

AWS Training in HRBR Layout
AWS Training in Kalyan Nagar
Best AWS Training Institute in kalyan Nagar Bangalore

Reply

I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation. Best AWS Training in Bangalore

Reply

It's good to see this blog to learn something. check it once through ASP .net.

.NET Training in Chennai | .NET Training Institute in Chennai

Reply

I am commenting to let you know what a terrific experience my daughter enjoyed reading through your web page. She noticed a wide variety of pieces, with the inclusion of what it is like to have an awesome helping style to have the rest without hassle grasp some grueling matters.
Hadoop Training in Chennai

Reply

Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage contribution from other ones on this subject while our own child is truly discovering a great deal. Have fun with the remaining portion of the year.
MEAN stack training in chennai

Reply

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Best Devops Training in pune
Microsoft azure training in Bangalore

Reply


It seems you are so busy in last month. The detail you shared about your work and it is really impressive that's why i am waiting for your post because i get the new ideas over here and you really write so well.

Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training
Selenium training in bangalore

Reply

Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support about this area.
Python training in bangalore
Python course in pune
Python training in bangalore

Reply

Nice Post! Thank you for sharing very good post, it was so Nice to read and useful to improve my knowledge as updated one, keep blogging.
Angular js Training in Electronic City

Reply

I am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing. Great efforts. I am quite sure I will learn much new stuff right here! Good luck for the next!Java training in Chennai

Java Online training in Chennai

Java Course in Chennai

Best JAVA Training Institutes in Chennai

Java training in Bangalore

Java training in Hyderabad

Java Training in Coimbatore

Java Training

Java Online Training

Reply

It is perfect time to make some plans for the future and it is time to be happy. I’ve read this post and if I could I desire to suggest you few interesting things or tips.highly informative and professionally written and I am glad to be a visitor of this perfect blog, thank you
selenium training in chennai

selenium training in chennai

selenium online training in chennai

software testing training in chennai

selenium training in bangalore

selenium training in hyderabad

selenium training in coimbatore

selenium online training

selenium training

Reply

Nice post. Thanks for sharing! I want people to know just how good this information is in your article. It’s interesting content and Great work.Very useful and informative content has been shared out here, Thanks for sharing it

Azure Training in Chennai

Azure Training in Bangalore

Azure Training in Hyderabad

Azure Training in Pune

Azure Training | microsoft azure certification | Azure Online Training Course

Azure Online Training

Reply

Thanks for sharing this wonderful content.its very useful to us.This is incredible,I feel really happy to have seen your webpage.I gained many unknown information, the way you have clearly explained is really fantastic.keep posting such useful information.keep posting such useful information.

IELTS Coaching in chennai

German Classes in Chennai

GRE Coaching Classes in Chennai

TOEFL Coaching in Chennai

spoken english classes in chennai | Communication training


Reply

Infycle Technologies, the best software training institute in Chennai offers the No.1 Data Science training in Chennai for Students, tech professionals, and freshers. In addition to the Data Science Training Course, Infycle also offers other professional courses such as Cyber Security, Python, Oracle, Java, Power BI, Digital Marketing, Big Data, etc., which will be trained with 100% practical classes. After the completion of training, the trainees will be sent for placement interviews in the top MNC's. Call 7502633633 to get more info and a free demo.

Reply