Jerry
Jerry

Reputation: 124

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.

Upvotes: 6

Views: 4843

Answers (4)

user7180153
user7180153

Reputation: 31

Just found this - too bad didn't find it earlier. A lot of built in options:

http://www.nudoq.org/#!/Packages/PagedList.Mvc/PagedList.Mvc/PagedListRenderOptions

and the one needed here is:

new PagedListRenderOptions{ Display = PagedListDisplayMode.IfNeeded }

http://www.nudoq.org/#!/Packages/PagedList.Mvc/PagedList.Mvc/PagedListRenderOptions/P/Display

Upvotes: 3

You Rule Avi
You Rule Avi

Reputation: 318

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 }))
  }
}

Upvotes: 3

AKD
AKD

Reputation: 3964

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 }))
}

Upvotes: 7

Jerry
Jerry

Reputation: 124

This solved the problem.

 @{
if(Model.PageCount > 1)
{
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, filter = ViewBag.Filter }))
}
}

Upvotes: 2

Related Questions