I am trying to add Pagination in ASP.NET MVC using this link(https://qawithexperts.com/article/asp.net/paging-sorting-and-filtering-in-aspnet-mvc-c/31)
return View(ModelList.ToPagedList(pageNumber, pageSize));
in my project when I added paging "ToPagedList" throws an error.
also get this error
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication3.TeleInfo]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[MvcApplication3.TeleInfo]'.
First, You need to Install this package using Nuget manager console(In VIsual Studio,Click on Tools-> NuGet Package Manager -> Package Manager Console) using this line
Install-Package PagedList.Mvc
After installing it reference it in your controller, your error of ToPagedList should get resolved.
For Second error,
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[MvcApplication3.TeleInfo]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[MvcApplication3.TeleInfo]'.
You need to change the @model type at the top of your view.
It should be changed from
@model IEnumerable<MvcApplication3.TeleInfo>
to
@model PagedList.IPagedList<MvcApplication3.TeleInfo]> @using PagedList.Mvc;
Read the article carefully, it is given there, it will resolve your error for sure, thanks
the error is may be because your Model is not returning .OrderBy
Data
I.e. instead of
return View(ModelList.ToPagedList(pageNum, pageSize));
You should change your code to:
return View(ModelList.OrderBy(x => x.Any_Column_Name_To_Order).ToPagedList(pageNumber, pageSize));
try the above changes, plus the changes mentioned in above answer, it should resolve both of your issue's, let me know by commenting if it works, thanks
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly