//Create a List for index of servers public ServerIndexVM GetServerList(string sortOrder, string currentFilter, string searchString, int?page) { try { IEnumerable <ServerListVM> serverList = _context.Server .Select(c => new ServerListVM { ServerName = c.ServerName, ReferenceID = c.ReferenceID, StatusName = c.Status.StatusName, ServerTypeName = c.ServerType.ServerTypeName, LocationName = c.DataCenterLocation.Location, Description = c.Description }); page = searchString == null ? page : 1; int currentPageIndex = page.HasValue ? page.Value - 1 : 0; searchString = searchString ?? currentFilter; int pageNumber = (page ?? 1); int defaultPageSize = ConstantsRepo.PAGE_SIZE; var sorted = Sort(serverList, sortOrder, searchString); int totalNumOfServers = sorted.Count(); // sort by status name default sortOrder = sortOrder ?? ConstantsRepo.SORT_SERVER_BY_NAME_DESC; ServerIndexVM model = new ServerIndexVM { Servers = sorted.ToPagedList(pageNumber, defaultPageSize), CurrentFilter = searchString, CurrentSort = sortOrder, TotalItemCount = totalNumOfServers, ItemStart = currentPageIndex * defaultPageSize + 1, ItemEnd = totalNumOfServers - (defaultPageSize * currentPageIndex) >= defaultPageSize ? defaultPageSize * (currentPageIndex + 1) : totalNumOfServers, DescriptionSort = sortOrder == ConstantsRepo.SORT_SERVER_BY_DESCRIPTION_DESC ? ConstantsRepo.SORT_SERVER_BY_DESCRIPTION_ASCE : ConstantsRepo.SORT_SERVER_BY_DESCRIPTION_DESC, StatusSort = sortOrder == ConstantsRepo.SORT_SERVER_BY_STATUS_NAME_DESC ? ConstantsRepo.SORT_SERVER_BY_STATUS_NAME_ASCE : ConstantsRepo.SORT_SERVER_BY_STATUS_NAME_DESC, LocationSort = sortOrder == ConstantsRepo.SORT_SERVER_BY_LOCATION_NAME_DESC ? ConstantsRepo.SORT_SERVER_BY_LOCATION_NAME_ASCE : ConstantsRepo.SORT_SERVER_BY_LOCATION_NAME_DESC, ServerTypeSort = sortOrder == ConstantsRepo.SORT_SERVERTYPE_BY_NAME_DESC ? ConstantsRepo.SORT_SERVERTYPE_BY_NAME_ASCE : ConstantsRepo.SORT_SERVERTYPE_BY_NAME_DESC, ServerNameSort = sortOrder == ConstantsRepo.SORT_SERVER_BY_NAME_DESC ? ConstantsRepo.SORT_SERVER_BY_NAME_ASCE : ConstantsRepo.SORT_SERVER_BY_NAME_DESC, }; return(model); } catch (Exception e) { if (e is SqlException) { } return(null); } }
public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page) { ServerIndexVM model = _serverRepo.GetServerList(sortOrder, currentFilter, searchString, page); return(View(model)); }