I'm sure MvcPaging is great, but I spent 2 hours bashing my head against the screen and couldn't get it to work, so I wrote my own implementation... which took less than 2 hours!
Controller code:
[Authorize]
public ActionResult ListNotes(int patientId, int? page = 1)
{
const int pageSize = 5;
var db = new PracwareEntities();
var totalNotes = db.patientnotes.Count(n => n.patientDbId == patientId);
var notes = db.patientnotes.OrderBy(o => o.dateTime).Skip((int)((page - 1) * pageSize)).Take(pageSize).Where(n => n.patientDbId == patientId).ToList();
ViewBag.PageCount = GetPageCount(totalNotes, pageSize);
ViewBag.CurrentPage = page;
ViewBag.patientId = patientId;
if (Request.IsAjaxRequest())
return PartialView("ListNotes", notes);
return View(notes);
}
private static int GetPageCount(int notesCount, int pageSize)
{
var x = (double) notesCount/pageSize;
if (x - (int) x > 0)
return (notesCount/pageSize) + 1;
return (notesCount/pageSize);
}
Razor view:
@if (ViewBag.PageCount > 1)
{
<div id="paging-control" class="pagination ">
<ul>
<li class="first">
@Html.ActionLink("First", "ListNotes", "Patient",
new {ViewBag.patientId, Page = "1" }, new {@Class="update-notes-list "} )
</li>
@for (var i = 1; i <= ViewBag.PageCount; i++)
{
<li @if (ViewBag.CurrentPage == i) {
<text> class="active"</text>
} >
@Html.ActionLink(i.ToString(), "ListNotes", "Patient",
new {ViewBag.patientId, Page = i.ToString() }, new {@Class="update-notes-list"})
</li>
}
<li class="last">
@Html.ActionLink("Last", "ListNotes", "Patient",
new { ViewBag.patientId, Page = ViewBag.PageCount }, new {@Class="update-notes-list "} )
</li>
</ul>
</div>
}
Not elegant, but when deadlines are approaching, a working solution is better than no solution!