public ActionResult Index(SearchExpensesModel search, int page = 1) { // Return all Expenses // If not a post-back (i.e. initial load) set the searchModel to session if (Request.Form.Count <= 0) { if (search.IsEmpty() && Session["SearchExpensesModel"] != null) { search = (SearchExpensesModel)Session["SearchExpensesModel"]; } } var helper = new ExpenseHelper(); var model = helper.GetExpenseList(search, search.ParsePage(page)); Session["SearchExpensesModel"] = search; return(View(model)); }
public ExpenseListViewModel GetExpenseList(SearchExpensesModel searchModel, int page = 1) { int pageSize = 20; if (page < 1) { page = 1; } IEnumerable <Expense> records = db.Expenses.ToList(); if (!String.IsNullOrEmpty(searchModel.Name)) { string name = searchModel.Name.ToLower(); records = records.Where(x => x.By.ToLower().Contains(name)); } if (searchModel.StartDate.HasValue) { records = records.Where(p => p.Date >= searchModel.StartDate); } if (searchModel.EndDate.HasValue) { records = records.Where(p => p.Date <= searchModel.EndDate); } if (searchModel.Category.HasValue) { records = records.Where(p => p.Category == searchModel.Category); } return(new ExpenseListViewModel { Expenses = records .OrderByDescending(o => o.Date) .Skip((page - 1) * pageSize) .Take(pageSize), SearchModel = searchModel, PagingInfo = new PagingInfo { CurrentPage = page, PageSize = pageSize, TotalItems = records.Count() } }); }