public ActionResult Index(int page = 1) { int pageSize = 5; // количество объектов на страницу if (User.Identity.IsAuthenticated) { ViewBag.Us = User.Identity.IsAuthenticated; using (CommonContext db = new CommonContext()) { var model = db.Ads.ToList(); model.Reverse(); IEnumerable<Ad> adsPerPages = model.Skip((page - 1) * pageSize).Take(pageSize); PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = model.Count }; IndexViewModel ivm = new IndexViewModel { PageInfo = pageInfo, Ads = adsPerPages }; ViewBag.IsUnread = IsUnread(); return View(ivm); } } return View("Guest"); }
public ActionResult preSearch(string Keywords, int page = 1) { ViewBag.Keywords = Keywords; using (CommonContext db = new CommonContext()) { List<Ad> sortedModel = new List<Ad>(); List<Ad> model; model = (from items in db.Ads select items).ToList(); if (Keywords != "") { if (model.Count != 0) { string[] keys = Keywords.Split(','); for (int i = 0; i < keys.Length; i++) { string key = keys[i].Trim(); foreach (var m in model) { if (m.Keywords != null) { if (m.Keywords.Contains(key)) { sortedModel.Add(m); } } } } } } else sortedModel = model; int pageSize = 2; IEnumerable<Ad> adsPerPages = model.Skip((page - 1) * pageSize).Take(pageSize); PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = model.Count }; IndexViewModel ivm = new IndexViewModel { PageInfo = pageInfo, Ads = sortedModel }; ViewBag.IsUnread = IsUnread(); return View("Search", ivm); } }
public ActionResult Search(string Keywords, string Theme, string Location, string Popularity, int page = 1) { //this.TempData["Keywords"] = Keywords; ViewBag.Keywords = Keywords; ViewBag.Theme = Theme; //ViewBag.Location = Location; ViewBag.Popularity = Popularity; using (CommonContext db = new CommonContext()) { List<Ad> sortedModel = new List<Ad>(); List<Ad> model; if (Theme != "Усі теми") model = (from items in db.Ads where items.Theme == Theme select items).ToList(); else model = (from items in db.Ads select items).ToList(); if (Location != "Україна") model = (from items in model where items.Country == Location select items).ToList(); else model = (from items in model select items).ToList(); if (Keywords != "") { if (model.Count != 0) { string[] keys = Keywords.Split(','); for (int i = 0; i < keys.Length; i++) { string key = keys[i].Trim(); foreach (var m in model) { if (m.Keywords != null) { if (m.Keywords.Contains(key)) { sortedModel.Add(m); } } } } } } else sortedModel = model; if (Popularity == "on") { List<Ad> helper = (from item in sortedModel orderby item.Views descending select item).ToList<Ad>(); sortedModel = helper; } int pageSize = 2; IEnumerable<Ad> adsPerPages = model.Skip((page - 1) * pageSize).Take(pageSize); PageInfo pageInfo = new PageInfo { PageNumber = page, PageSize = pageSize, TotalItems = model.Count }; IndexViewModel ivm = new IndexViewModel { PageInfo = pageInfo, Ads = sortedModel }; //sortedModel.Reverse(); ViewBag.IsUnread = IsUnread(); return View(ivm); } }