asp.net-mvcasp.net-mvc-4pagination

How to send current page number in MVC4 sorting?


Hi I have implemented paging in my MVC application and I am implementing sorting facility. When i click on header(ActionLink) i want to sort. This is my pager code.

  @Html.PagedListPager(Model.logDetails, page => Url.Action("Index",
new { page, currentFilter = ViewBag.CurrentFilter, filterdateTime=ViewBag.filterdateTime, filterdocType= Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId, sortOrder = ViewBag.currentSort }))
                Page @(Model.logDetails.PageCount < Model.logDetails.PageNumber ? 0 : Model.logDetails.PageNumber) of @Model.logDetails.PageCount

This is my header where i want sort.

 <th>@Html.ActionLink("Label", "Index", new { sortOrder = ViewBag.LabelSortParm, currentFilter = ViewBag.CurrentFilter, filterdateTime = ViewBag.filterdateTime, filterdocType = Model.doc_typeid, filteredemployeeID = Model.employeeID, filteredcitizenId = Model.citizenId, Page })</th>

Here I am having problem with sending current page number back to action method. All the values I am able to send it back to the action method except current page number. Can anyone tell me how can i get and send pagenumber in ActionLink? Thank you in advance.


Solution

  • At the bottom of you razor view page something like it should there for the page navigation. In that case watch out for the parameter called page. when ever navigation request has been made from UI, that will pass this current count to Controller Action result method.

    <!--Grid / Page navigations goes here-->
    <br />
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
    @Html.PagedListPager(Model, page => Url.Action("List", new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))
    

    The same way you should be having your controller logic similar to this. Here also look at the parameter called page. which carries the current page number all the time. as we passed from UI. By default it i will be 0 OR 1.

        public ViewResult List(string sortOrder, string currentFilter, string searchString, int? page, ManageMessage? message)
        {
                ViewBag.CurrentSort = sortOrder;
                ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" : "";
                if (searchString != null) { page = 1; }
                else { searchString = currentFilter; }
    
                ViewBag.CurrentFilter = searchString;
    
                var company = getComoanyList();
    
                switch (sortOrder)
                {
                    case "Name_desc":
                        company = company.OrderByDescending(s => s.Name);
                        break;
                    default:
                        company = company.OrderBy(s => s.Name);
                        break;
                }
    
                int pageSize = 10;
                int pageNumber = (page ?? 1);
                return View(company.ToPagedList(pageNumber, pageSize));
        }
    

    Hope this helps.