Click here to Skip to main content
14,241,575 members
Rate this:
Please Sign up or sign in to vote.
I have my pagination with the view and the action with the view name correct and working, Now I have another action that gets a custom set of data that uses the same model in the view just with different results. How can I make my pagination work with this separate action. In the separate action I originally called View(“viewname”, Model) to open my view with this data.
public async Task<ActionResult> UserTimeSheets(string id, int? page)
            var userTimeSheets = await (from times in context.TimeSheetMaster
                                  where times.UserId == id
                                  select new TimeSheetMasterModel()
                                      TimeSheetMasterId = times.TimeSheetMasterId,
                                      UserId = times.UserId.ToString(),
                                      IdShortened = times.UserId.Substring(0,10)
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            TempData["PanelHeader"] = "Your TimeSheets";
            return View(userTimeSheets.ToPagedList(pageNumber, pageSize));
        } // Here is the action that loads only the first page of data                      public async Task<ActionResult> UserApproveTimes(string userid, int? page)
            var approved = await (from m in context.TimeSheetMaster
                                  where m.TimeSheetStatus == 2
                                  && m.UserId == userid
                                  select new TimeSheetMasterModel()
                                      TimeSheetMasterId = m.TimeSheetMasterId,                                      
                                      IdShortened = m.UserId.Substring(0,8),
                                      TotalHours = m.TotalHours,
                                      Comment = m.Comment,
            int pageSize = 3;
            int pageNumber = (page ?? 1);
            TempData["PanelHeader"] = "Your Approved TimeSheets";
            return View("UserTimeSheets", approved.ToPagedList(pageNumber, pageSize));
        }	// Here is the view parts of it                                               @model PagedList.IPagedList<MVC_TimeSh.Models.TimeSheetMasterModel>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
                    @*if (TempData["PageHeader"] == "Your Approved TimeSheets")
                        @Html.PagedListPager(Model, page => Url.Action("UserApproveTimes", new { page }))
                        @Html.PagedListPager(Model, page => Url.Action("UserTimeSheets", new { page }))
                    @Html.PagedListPager(Model, page => Url.Action("UserTimeSheets", new { page = page}),
                   PagedListRenderOptions.EnableUnobtrusiveAjaxReplacing(new AjaxOptions()
                       HttpMethod = "GET",
                       UpdateTargetId = "table"

What I have tried:

. I can get the pagination to show the correct number of pages and loads the first page correctly but as soon as I click the next page it shows no data in the view. I am using PagedList for my pagination. Here is my code with pagination working and the action that shows the first page of data. I tried using ajax thinking maybe itll work because the page doesn’t need to refresh but it does the same thing as before the 2nd page is empty.
Christian Graus 2-May-19 21:24pm
Have you stepped through the code. Sounds like you're not sending page numbers correctly?
TheBigBearNow 2-May-19 21:58pm
The first page works correctly.
The page with the same action name as the view works on all the pageination links
The only ones that dont work is the action that calls the view the view and loads it with a custom set of data when i click the pagination link for that one the 2nd page is empty

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100