private CreditTypesViewModel GetCreditTypesViewModelForPage(int pageNumber, double percentFrom, double percentTo, int payCountFrom, int payCountTo) { int itemsInPage = 10; var list = _creditTypesService.GetList(); if (list == null) { return new CreditTypesViewModel() { SearchResult = false }; } { var tPercentFrom = (percentFrom != 0 ? percentFrom : double.MinValue); var tPercentTo = (percentTo != 0 ? percentTo : double.MaxValue); var tPayCountFrom = (payCountFrom != 0 ? payCountFrom : int.MinValue); var tPayCountTo = (payCountTo != 0 ? payCountTo : int.MaxValue); list = list.Where(x => x.Percent >= tPercentFrom && x.Percent <= tPercentTo).ToList(); list = list.Where(x => x.PayCount >= tPayCountFrom && x.PayCount <= tPayCountTo).ToList(); } if (list.Count == 0) { return new CreditTypesViewModel() { SearchResult = false }; } int startRange = pageNumber * 10 - itemsInPage; int allPageCount = list.Count / itemsInPage; int ost = list.Count % itemsInPage; if (ost != 0) { allPageCount++; } int selectCount = ((pageNumber >= allPageCount && ost != 0) ? ost : itemsInPage); if (list.Count != 0) { list = list.OrderBy(x => x.Created).ToList(); list = list.GetRange(startRange, selectCount); } var viewModel = new CreditTypesViewModel() { CreditTypes = list.Select( creditType => new CreditTypeViewModel() { Id = creditType.Id, Name = creditType.Name, Percent = creditType.Percent, StartSumPercent = creditType.StartSumPercent, PayCount = creditType.PayCount, Info = creditType.Info, SubTypes = _creditTypesService.GetCreditSubTypes().Select(x => new SelectListItem() { Value = x.Id.ToString(), Text = x.Name }) }).ToList(), CurrentPageNumber = pageNumber, AllPageCount = allPageCount, ItemsPerPage = itemsInPage, PayCountFrom = payCountFrom, PayCountTo = payCountTo, PercentFrom = percentFrom, PercentTo = percentTo, SearchResult = true }; return viewModel; }
// GET: CreditTypes public ActionResult Index(CreditTypesViewModel model) { var viewModel = GetCreditTypesViewModelForPage(model.CurrentPageNumber, model.PercentFrom, model.PercentTo, model.PayCountFrom, model.PayCountTo); if (viewModel.SearchResult == false) { ViewBag.Result = false; ViewBag.ResultMsg = "error load requests"; } return View(viewModel); }