Reputation: 1
I have problems in displaying page on how to include the prev and next link... I am using ASP.Net MVC 4 using these codes:
VIEW:
<div class="pagination">
<ul>
@for (int i = 1; i <= ViewBag.PagesCount; i++)
{
<text>
<li>@Html.ActionLink(i.ToString(), "Index", new { pagee = i })</li>
</text>
}
</ul>
</div>
CONTROLLER:
int offset = 15;
int pagecount = ((pagee - 1) * offset);
int totalPages = 0;
int totalItems = 0;
try
{
totalItems = requestform.Count();
while (totalItems > 0)
{
totalItems -= 15;
totalPages++;
}
ViewBag.PagesCount = totalPages;
return View(requestform.Skip(pagecount).Take(offset).ToList());
}
catch
{
while (totalItems > 0)
{
totalItems -= 15;
totalPages++;
}
ViewBag.PagesCount = totalPages;
return View(requestform.Skip(pagecount).Take(offset).ToList());
}
Please help me with this.
Upvotes: 0
Views: 10751
Reputation: 6858
recently i found with cool controls, tooltip, first, next, last, previous check this mvc 4 paging nuget package, demo web page
Upvotes: 0
Reputation: 599
I would recommend also PagedList just add it via NuGet package manager and then,
modify your controller to look like this:
public ActionResult Index(int? page)
{
var dbtable = db.DbTable.Include(s => s.ID).OrderBy(s => s.StyleName); //ensure records are sorted.
if (Request.HttpMethod != "GET")
{
page = 1;
}
int pageSize = 2;
int pageNumber = (page ?? 1);
return View(dbtable.ToPagedList(pageNumber, pageSize));
}
in view replace:
@model IEnumerable<MyMvcApplication.Models.dbtable>
with
@model PagedList.IPagedList<MyMvcApplication.Models.dbtable>
replace code that looks like
<th>
@Html.DisplayNameFor(model => model.ID)
</th>
with
<th>
@Html.DisplayNameFor(model => model.First().ID)
</th>
at the end of the page after the </table>
tag add this:
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { page = 1})
@Html.Raw(" ");
@Html.ActionLink("< Prev", "Index", new {page = Model.PageNumber - 1})
}
else{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next >", "Index", new {page = Model.PageNumber + 1})
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new {page = Model.PageCount})
}
else{
@:Next >
@Html.Raw(" ")
@:>>
}
</div>
Upvotes: 0
Reputation: 46
To add paging to page, you'll start by installing the PagedList NuGet package. Then you'll make additional changes in the Index method and add paging links to the Index view
Please check this link http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
Upvotes: 0