Part 63 Implement paging in asp net mvc

preview_player
Показать описание
Link for code samples used in the demo

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Step 1: Install PagedList.Mvc using NuGet package manager. PagedList.Mvc is dependent on PagedList. Installing PagedList.Mvc will automatically install PagedList package as well.

using PagedList.Mvc;
using PagedList;

Modify the Index() action method as shown below. Notice that we are passing page parameter to this function. This parameter is used for specifying the page number. This parameter can be null, and that's the reason we have chosen a nullable integer. We convert the list, to a paged list, using ToPagedList(). Also, notice that, we are using null-coalescing operator. If the "page" parameter is null, then 1 is passed as the page number, else, the value contained in the "page" parameter is used as the page number.
public ActionResult Index(string searchBy, string search, int? page)
{
if (searchBy == "Gender")
{
return View(db.Employees.Where(x =] x.Gender == search || search == null).ToList().ToPagedList(page ?? 1, 3));
}
else
{
return View(db.Employees.Where(x =] x.Name.StartsWith(search) || search == null).ToList().ToPagedList(page ?? 1, 3));
}
}

a) Include the following 2 using statements on the view.
@using PagedList.Mvc;
@using PagedList;

b) The model for the view should be IPagedList[Employee].
@model IPagedList[MVCDemo.Models.Employee]

c) Since, we have changed the model of the view, from IEnumerable[MVCDemo.Models.Employee] to IPagedList[MVCDemo.Models.Employee], change the section that displays table headings as shown below.
[tr]
[th]
@Html.DisplayNameFor(model =] model.First().Name)
[/th]
[th]
@Html.DisplayNameFor(model =] model.First().Gender)
[/th]
[th]
@Html.DisplayNameFor(model =] model.First().Email)
[/th]
[th]Action[/th]
[/tr]

d) Finally to display page numbers for paging
@Html.PagedListPager(Model, page =] Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }))

e) If you want to display the pager, only if there are more than 1 page
@Html.PagedListPager(Model, page =] Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded })

f) If you want to display, the current active page and the total number of pages
@Html.PagedListPager(Model, page =] Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayPageCountAndCurrentLocation = true })

g) If you want to display the number of rows displayed, of the total number of rows available.
@Html.PagedListPager(Model, page =] Url.Action("Index", new { page, searchBy = Request.QueryString["searchBy"], search = Request.QueryString["search"] }), new PagedListRenderOptions() { Display = PagedListDisplayMode.IfNeeded, DisplayItemSliceAndTotal = true })

Make sure to replace [ with LESSTHAN and ] with GREATERTHAN symbol.
Рекомендации по теме
Комментарии
Автор

I couldnt find a way to send a parameter to pagedlist, It really helped me. Thank you.

hasanansari
Автор

Sure, will try to record a video on this as soon as I can.

Csharp-video-tutorialsBlogspot
Автор

Excellent videos, very useful and so much clear.

herbertgonzalez
Автор

A lot of thanks sir I get all solution whatever i have needed .

dollykumari
Автор

Thanks a lot! It was very useful for me.

djoscarpino
Автор

At first Thanks for great tutorials!
And i have a question that what if the number of "querystring" is more than 30?

shohratmaharram
Автор

Hi when I search using name or gender I could not able to retain the page .every time I have to copy paste the controller and action name inorder to get search result..

bharathb
Автор

Lots of thanks Sir. is this demo possible using ajax.

varunpujara
Автор

HI rShekdar Im also faced same problem.the soln is just drag and drop the css style sheet(PagedList.css) which present in the content folder to your required view.Then You will see the actual Output.

ajaybabu.d
Автор

kudvenkat Sir, This is a marvellous video for paging for sure, but these paging does not work at all in case of Ajax forms and what I have seen that people mostly develop paging in the in Ajax atleast in my case, I would request you to please create the same video for paging in Ajax forms and the same has already requested by
Thanks in Advance

MohammedDawoodAnsari
Автор

will this work in asp dot net core mvc ?

rajankhadka
Автор

How can I use the PagedList without reloading the entire page?

ciclonteam
Автор

hi venkat ji, could u teach us how to implement paging using dropdownlist pls

harikaaakutota
Автор

in my project there is occuring one problem
can u solve it?

adnanaddy
Автор

thus appears in VS 2012 :(

Page 1 of 2.
1
2
»

pablosalas
Автор

thanks a lot but  i'm getting this error please help me
Error:Method not found: 'Int32

ramiganimadhukarreddy
Автор

How to work on paging in MVC without using Entity Framework.

srinivasbolisetti
Автор

I got trouble with Multiple Model :\
any body help me ???

QuyetNguyen-iejc
Автор

I did not understand why it is showing like that.. Not nearly

Page 1 of 3.
1
2
3
»

hendesebilisim
Автор

Get confused by 'PagedListPager'

Wowokissy