Jerry Jerry - 14 days ago 5
C# Question

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.

Answer

This solved the problem.

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