public IActionResult List(string searchvalue, int pageindex = 1, int pagesize = 10, int nextpage = 10, string sortlink = "ID") { Console.WriteLine(searchvalue); var k = ViewBag.pagesize; ListViewModel vm = new ListViewModel(); //if (hiddennewpagesize != 0) { pagesize = hiddennewpagesize; } int originalpageindex = pageindex; decimal totalpages = 0; decimal totalcount = _irequestdata.ListBySearch(searchvalue).Count(); decimal runningvalue = (totalcount / pagesize); totalpages = Math.Ceiling(runningvalue); int gettruecount = _irequestdata.ListBySearch(searchvalue).ToList().Count(); gettruecount = (pageindex - 1) * pagesize - gettruecount; if (pageindex > 0) { pageindex = pageindex * pagesize - pagesize; } if (gettruecount > 0) { pagesize = gettruecount - pagesize; } if (gettruecount < 0 & originalpageindex == totalpages) { pagesize = -1 * gettruecount; } Console.WriteLine(pagesize); Console.WriteLine(pageindex); var data = _irequestdata.ListBySearch(searchvalue).OrderBy(x => x.Id).ToList().GetRange(pageindex, pagesize); if (ViewData["sortseq"] != null) { if (ViewData["sortseq"].ToString() == "ASC") { switch (sortlink) { case "Date Created": data = _irequestdata.ListBySearch(searchvalue).OrderBy(x => x.DateCreated).ToList().GetRange(pageindex, pagesize); break; case "Date Required": data = _irequestdata.ListBySearch(searchvalue).OrderBy(x => x.DateRequired).ToList().GetRange(pageindex, pagesize); break; case "CFT Team": data = _irequestdata.ListBySearch(searchvalue).OrderBy(x => x.CFTTeam).ToList().GetRange(pageindex, pagesize); break; case "Qty Required": data = _irequestdata.ListBySearch(searchvalue).OrderBy(x => x.QtyRequired).ToList().GetRange(pageindex, pagesize); break; case "Creater": data = _irequestdata.ListBySearch(searchvalue).OrderBy(x => x.Creater).ToList().GetRange(pageindex, pagesize); break; } } if (ViewData["sortseq"].ToString() == "DSC") { switch (sortlink) { case "ID": data = _irequestdata.ListBySearch(searchvalue).OrderByDescending(x => x.Id).ToList().GetRange(pageindex, pagesize); break; case "Date Created": data = _irequestdata.ListBySearch(searchvalue).OrderByDescending(x => x.DateCreated).ToList().GetRange(pageindex, pagesize); break; case "Date Required": data = _irequestdata.ListBySearch(searchvalue).OrderByDescending(x => x.DateRequired).ToList().GetRange(pageindex, pagesize); break; case "CFT Team": data = _irequestdata.ListBySearch(searchvalue).OrderByDescending(x => x.CFTTeam).ToList().GetRange(pageindex, pagesize); break; case "Qty Required": data = _irequestdata.ListBySearch(searchvalue).OrderByDescending(x => x.QtyRequired).ToList().GetRange(pageindex, pagesize); break; case "Creater": data = _irequestdata.ListBySearch(searchvalue).OrderByDescending(x => x.Creater).ToList().GetRange(pageindex, pagesize); break; } } } //if (ViewData["sortseq"] != null) //{ // if (ViewData["sortseq"].ToString() == "DSC") // { // data = _irequestdata.ListBySearch(searchvalue).OrderByDescending(x => x.Id).ToList().GetRange(pageindex, pagesize); // } //} ViewBag.pagelist = totalpages; ViewBag.pagenumber = originalpageindex; vm.SPAHeaders = data; vm.nextpagenumber = nextpage; vm.startpage = originalpageindex; vm.pagesize = pagesize; vm.totalpages = Convert.ToInt32(totalpages); //data = _irequestdata.HeaderRequests.ToList().GetRange(2, 2); return(View(vm)); }