Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
C#
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)
                                  }).ToListAsync();
            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,
                                  }).ToListAsync();
            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 }))
                    }
                    else
                    {
                        @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.
Posted
Comments
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900