Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid paging if there is only 1 page in mvc

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.

like image 266
Jerry Avatar asked Apr 02 '14 06:04

Jerry


2 Answers

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 }))
}
like image 144
Ashok Damani Avatar answered Nov 14 '22 22:11

Ashok Damani


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 }))
  }
}
like image 32
You Rule Avi Avatar answered Nov 14 '22 21:11

You Rule Avi