-1

How to do pagination in C# MVC?

I'm trying to do something like this,

I want retrieve 1000 records from database and keep it in server side then i want to send 10 records per each page to view when user requesting the searched page.

I have gone through some examples but i couldn't get better solution, I prefer that the way i suggested or something better solution for this. (I'm using jquery as well and not using Razor)

example which I gone through

Paginated search results with LINQ to SQL

Thanks in advance

Community
  • 1
  • 1
Fazal Mohamed
  • 121
  • 1
  • 7
  • 15
  • There's a package from Microsoft called WebMatrix that has a WebGrid in it that supports paging, you could give that a try. http://msdn.microsoft.com/en-us/magazine/hh288075.aspx – asymptoticFault Sep 13 '13 at 13:37

2 Answers2

0

Paging in ASP.NET MVC is a solved problem by some libraries; you can find them at Nuget.org by http://www.nuget.org/packages?q=paging.

I want retrieve 1000 records from database and keep it in server side then i want to send 10 records per each page to view when user requesting the searched page.

Retrieving 1000 records and keep them in your application wouldn't be ideal. It would be better to retrieve 10 records in every request; as you have mentioned in your question, the solution that use Skip and Take in a linq query is perfect. However, there is no problem to page cached data.

Abbas Amiri
  • 3,074
  • 1
  • 23
  • 23
0

Try This One of the easiest method

Controller

using PagedList;
public ActionResult Index(int ? pagePos)
{
        //return View(db.Items.ToList());// Change this as following
        int pageNumber = (pagePos ?? 1);
        return View(db.Items.ToList().ToPagedList(pageNumber, 30)); //30 is the size of records in a single page
}

Index.cshtml

@*@model IEnumerable<ProjectDB.Models.Item>*@
@*Change These Also as following*@
@model PagedList.IPagedList<ProjectDB.Models.Item>
@using PagedList.Mvc


<table class="table">
<tr>
    <th>
        Name
    </th>
    <th>
        ID
    </th>

    <th></th>
</tr>

@foreach (var item in Model) {
<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.ID)
    </td>

    <td>
        @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
        @Html.ActionLink("Details", "Details", new { id=item.Id }) |
        @Html.ActionLink("Delete", "Delete", new { id=item.Id })
    </td>
</tr>
}   
</table>

@*Pagination Code Starts*@

<div class="pageCount">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
</div>
    @Html.PagedListPager(Model, pagePos => Url.Action("Index", new { pagePos }))
Jishnu KM
  • 234
  • 1
  • 8