I am using paging to list down Cadidates from Database. I am listing out 12 record in a page. I dont want paging to be shown if have only 1 page with details to be listed. Pls Help me out. The following is my code. Contoller:
[HttpGet]
    public ActionResult Index(int? page, int? filter)
    {
        ViewBag.statusName = db.CandidateStatuses.ToList();
        int pageSize = 12;
        int pageNumber = (page ?? 1);
        var candidates = new List<Candidate>();
        if (filter != null)
        {
            ViewBag.Filter = filter;
            candidates = db.Candidates.Where(m => m.CandidateStatusID == filter).OrderByDescending(m => m.CandidateID).ToList();
        }
        else
        {
            candidates = db.Candidates.OrderByDescending(m => m.CandidateID).ToList();
        }
        return View(candidates.ToPagedList(pageNumber, pageSize));
    }
View:
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, filter = ViewBag.Filter }))
I Have Displayed only the code for paging part in my view. Pls help me out.
I think it should work:
@if(Model.PageCount > 1)
{
  Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
  @Html.PagedListPager(Model, page => Url.Action("Index", new { page, filter = ViewBag.Filter }))
}
                        Stack overflow, rejected my edit, and suggested I add it as an answer.
For fixing the whole section, add a < p > tag around the "Page X of Y ". That will avoid the "parser error" mentioned in the comment section above.
@{if(Model.PageCount > 1)
  {
  <p>Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount<p>
  @Html.PagedListPager(Model, page => Url.Action("Index", new { page, filter = ViewBag.Filter }))
  }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With