Error implementing Paging in MVC C#


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]'.

 


Asked by:- Aiswarjya
0
: 5642 At:- 9/8/2017 11:57:57 AM
mvc Razor IPagedList







2 Answers
profileImage Answered by:- pika

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

3
At:- 9/8/2017 1:21:36 PM
1) i installed Package PagedList.Mvc 2)already added it @model PagedList.IPagedList @using PagedList.Mvc; i got an error in controller return View(ModelList.ToPagedList(pageSize,pageNumber)); Here ToPagedList throws an error. 0
By : Aiswarjya - at :- 9/8/2017 1:46:17 PM
was missing reference of PagedList in controller, adding it , solved the issue, thanks 1
By : Aiswarjya - at :- 9/9/2017 8:04:28 AM


profileImage Answered by:- vikas_jk

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

0
At:- 9/8/2017 3:46:56 PM
.ToPagedList show with a red commentline. 0
By : Aiswarjya - at :- 9/9/2017 4:59:00 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use