Welcome to ASP.NET MVC Tutorial on How to make Paging. We are going to create a paging with help of PagedList.
At first you need to install pagedlist.mvc from package manager console with this code.
install-package pagedlist.mvc
Other Code are as follows:
using PagedList;
-----------------Home Controller code----- ------
public ActionResult Index(int? page)
{
StudentContext Db = new StudentContext();//create object of context
List<Student> students = new List<Student>();
students = Db.Students.ToList();
int pageSize = 3;
int pageNumber = (page ?? 1);
return View(students.ToPagedList(pageNumber, pageSize));
}
--------------------------------------------------------
-------------View Code--------Index.cshtml---------------
@*@model IEnumerable<ASPtutorial.Models.Student>*@
@using PagedList.Mvc;
@model PagedList.IPagedList<ASPtutorial.Models.Student>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
Student Name
</th>
<th>
Address
</th>
<th>
Phone
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.sName)
</td>
<td>
@Html.DisplayFor(modelItem => item.sAddress)
</td>
<td>
@Html.DisplayFor(modelItem => item.sPhone)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.sId }) |
@Html.ActionLink("Details", "Details", new { id=item.sId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.sId })
</td>
</tr>
}
</table>
<br />
<div style="margin:5px;">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index",
new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
</div>
If you have still Doubt then watch this video to be more clear. For onlice classes contact in facebook.
fb.com/ythecoder
No comments:
Post a Comment