public ActionResult Index(int page = 0, int pageSize = -1, bool partial = false, string orderBy = null) { bool redirect = false; if (page <= 0) { page = 1; redirect = true; } if ((pageSize < 0)) { pageSize = 3; redirect = true; } ViewBag.IdSort = string.IsNullOrEmpty(orderBy) ? "Id desc" : ""; ViewBag.AcrSort = "Acr desc".Equals(orderBy) ? "Acr" : "Acr desc"; ViewBag.NameSort = "Name desc".Equals(orderBy) ? "Name" : "Name desc"; ViewBag.EctsSort = "Ects desc".Equals(orderBy) ? "Ects" : "Ects desc"; ViewBag.OwnerSort = "Owner desc".Equals(orderBy) ? "Owner" : "Owner desc"; ViewBag.PageSize = pageSize; ViewBag.OrderBy = orderBy; var elems = RepositoryLocator.Get<long, Proposal>() .GetAll() .Where(p => User.IsInRole("Admin") ? p.State.Equals(AbstractEntity<long>.Status.Pending) : p.Owner.Equals(User.Identity.Name)); switch(orderBy) { case "Id desc": elems = elems.OrderByDescending(f => f.Key); break; case "Acr": elems = elems.OrderBy(f => f.Info.Key); break; case "Acr desc": elems = elems.OrderByDescending(f => f.Info.Key); break; case "Name": elems = elems.OrderBy(f => f.Info.Name); break; case "Name desc": elems = elems.OrderByDescending(f => f.Info.Name); break; case "Ects": elems = elems.OrderBy(f => f.Info.Ects); break; case "Ects desc": elems = elems.OrderByDescending(f => f.Info.Ects); break; case "Owner": elems = elems.OrderBy(f => f.Owner); break; case "Owner desc": elems = elems.OrderByDescending(f => f.Owner); break; default: elems = elems.OrderBy(f => f.Key); break; } var viewModel = new TableViewModel { Items = pageSize > 0 ? elems.Skip((page - 1)*pageSize) //Salta os elementos iniciais que não interessam .Take(pageSize) //Retorna apenas o numero de elementos que pretendemos : elems, PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = elems.Count() } }; int total = viewModel.PagingInfo.TotalPages; if (page > total) { page = total; redirect = true; } if (redirect) return RedirectToAction("Index", new {page, pageSize}); return partial ? (ActionResult)PartialView("ProposalsTableContent", viewModel.Items as IEnumerable<Proposal>) : View(viewModel); }
public ActionResult Index(int page = 0, int pageSize = -1, bool partial = false, string orderBy = null) { bool redirect = false; if (page <= 0) { page = 1; redirect = true; } if (pageSize < 0) { pageSize = 3; redirect = true; } ViewBag.AcrSort = string.IsNullOrEmpty(orderBy) ? "Acr desc" : ""; ViewBag.NameSort = "Name desc".Equals(orderBy) ? "Name" : "Name desc"; ViewBag.EctsSort = "Ects desc".Equals(orderBy) ? "Ects" : "Ects desc"; ViewBag.PageSize = pageSize; ViewBag.OrderBy = orderBy; var elems = RepositoryLocator.Get<string, CurricularUnit>().GetAll(); switch (orderBy) { case "Acr desc": elems = elems.OrderByDescending(f => f.Key); break; case "Name": elems = elems.OrderBy(f => f.Name); break; case "Name desc": elems = elems.OrderByDescending(f => f.Name); break; case "Ects": elems = elems.OrderBy(f => f.Ects); break; case "Ects desc": elems = elems.OrderByDescending(f => f.Ects); break; default: elems = elems.OrderBy(f => f.Key); break; } var viewModel = new TableViewModel { Items = pageSize > 0 ? elems.Skip((page - 1)*pageSize) //Salta os elementos iniciais que não interessam .Take(pageSize) //Retorna apenas o numero de elementos que pretendemos : elems, PagingInfo = new PagingInfo { CurrentPage = page, ItemsPerPage = pageSize, TotalItems = elems.Count() } }; int total = viewModel.PagingInfo.TotalPages; if (page > total) { page = total; redirect = true; } if (redirect) return RedirectToAction("Index", new {page, pageSize}); return partial ? (ActionResult) PartialView("CurricularUnitsTableContent", viewModel.Items as IEnumerable<CurricularUnit>) : View(viewModel); }