0 Replies - 262 Views - Last Post: 02 May 2019 - 06:06 PM

#1 Exceedinglife   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 216
  • Joined: 01-July 12

Mvc5 pagination on view calling different action that populates View

Posted 02 May 2019 - 06:06 PM

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. 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.
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"

Is This A Good Question/Topic? 0
  • +

Page 1 of 1