public async Task <IActionResult> Index(int?page) { int pageNo = page ?? 1; var HelpManualDbContext = _context.FormObject .Include(f => f.ObjectType.ControlType) .OrderBy(x => x.Order); List <int> pages = new List <int>(); foreach (var fo in HelpManualDbContext) { pages.Add(fo.PageNo); } int?maxPage = HttpContext.Session.GetInt32("MaxPage"); //Remembers the max page using a session variable so that if the user goes back to another page //the page that they left will still be enabled if (maxPage == null) { HttpContext.Session.SetInt32("MaxPage", pageNo); } else if (maxPage < pageNo) { HttpContext.Session.SetInt32("MaxPage", pageNo); } ViewBag.MaxPage = HttpContext.Session.GetInt32("MaxPage"); string userName = HttpContext.User.Identity.Name.ToLowerInvariant(); _context.Add(new UserAccess(userName, pageNo)); await _context.SaveChangesAsync(); return(View(await PaginatedList <FormObject> .CreateAsyncHomePage(HelpManualDbContext.AsNoTracking(), pageNo, pages.Distinct().Count()))); }