public IActionResult Index(int?id, int?page) { ViewBag.Mechanisms = _context.Mechanisms.Where(m => m.HasDeleted == false).ToList(); ViewBag.CaseThicks = _context.CaseThicks.Where(m => m.HasDeleted == false).ToList(); ViewBag.GlassTypes = _context.GlassTypes.Where(m => m.HasDeleted == false).ToList(); ViewBag.BandTypes = _context.BandTypes.Where(m => m.HasDeleted == false).ToList(); ViewBag.WaterProtection = _context.WaterProtections.ToList(); ViewBag.Categories = _context.Categories.ToList(); ViewBag.Brands = _context.Brands.Where(b => b.HasDeleted == false).ToList(); List <BasketVM> products = new List <BasketVM>(); if (id != null) { //Response.Cookies.Delete("filter"); //Response.Cookies.Append("filter",) ViewBag.PageCount = Decimal.Ceiling((decimal)_context.Products .Where(t => t.HasDeleted == false && t.BrandId == id).Count() / 8); ViewBag.Page = page; if (page == null) { //Response.Cookies.Append("filter",Json) var pr = _context.Products.Where(t => t.HasDeleted == false && t.BrandId == id).Take(8).ToList(); foreach (Product item in pr) { BasketVM basketVM = new BasketVM { Id = item.Id, Model = item.Model, WatchCode = item.WatchCode, Price = item.Price, Image = item.Image, Discount = item.Discount }; products.Add(basketVM); } return(View(products)); } var pro = _context.Products.Where(t => t.HasDeleted == false && t.BrandId == id).Skip(((int)page - 1) * 8).Take(8).ToList(); foreach (Product item in pro) { BasketVM basketVM = new BasketVM { Id = item.Id, Model = item.Model, WatchCode = item.WatchCode, Price = item.Price, Image = item.Image, Discount = item.Discount }; products.Add(basketVM); } return(View(products)); } //List<BasketVM> basketproducts = new List<BasketVM>(); //if (Request.Cookies["basket"] != null) //{ // basketproducts = JsonConvert.DeserializeObject<List<BasketVM>>(Request.Cookies["basket"]); //} //ViewBag.BasketCount = basketproducts.Count(); //if (Request.Cookies["filter"] != null) //{ //} if (Request.Cookies["filter"] != null) { FilterOptionsVM optionsVM = new FilterOptionsVM(); optionsVM = JsonConvert.DeserializeObject <FilterOptionsVM>(Request.Cookies["filterOptions"]); ViewBag.PriceFrom = optionsVM.PriceFrom; ViewBag.PriceTo = optionsVM.PriceTo; ViewBag.CategoryId = optionsVM.CategoryId; ViewBag.BrandId = optionsVM.BrandId; ViewBag.MechanismId = optionsVM.MechanismId; ViewBag.WaterProtectionId = optionsVM.WaterProtectionId; ViewBag.BandTypeId = optionsVM.BandTypeId; ViewBag.CaseThicksId = optionsVM.CaseThicksId; ViewBag.GlassTypeId = optionsVM.GlassTypeId; products = JsonConvert.DeserializeObject <List <BasketVM> >(Request.Cookies["filter"]); ViewBag.PageCount = Decimal.Ceiling((decimal)products.Count() / 8); ViewBag.Page = page; if (page == null) { var pr = products.Take(8).ToList(); return(View(pr)); } var produ = products.Skip(((int)page - 1) * 8).Take(8).ToList(); return(View(produ)); } ViewBag.PageCount = Decimal.Ceiling((decimal)_context.Products .Where(t => t.HasDeleted == false).Count() / 8); ViewBag.Page = page; if (page == null) { var pr = _context.Products.Where(t => t.HasDeleted == false).Take(8).ToList(); foreach (Product item in pr) { BasketVM basketVM = new BasketVM { Id = item.Id, Model = item.Model, WatchCode = item.WatchCode, Price = item.Price, Image = item.Image, Discount = item.Discount }; products.Add(basketVM); } return(View(products)); } var prod = _context.Products.Where(t => t.HasDeleted == false).Skip(((int)page - 1) * 8).Take(8).ToList(); foreach (Product item in prod) { BasketVM basketVM = new BasketVM { Id = item.Id, Model = item.Model, WatchCode = item.WatchCode, Price = item.Price, Image = item.Image, Discount = item.Discount }; products.Add(basketVM); } return(View(products)); }
public IActionResult ProductFilter(int?PriceFrom, int?PriceTo, int?CategoryId, int?BrandId, int?MechanismId, int?WaterProtectionId, int?BandTypeId, int?CaseThickId, int?GlassTypeId) { FilterOptionsVM optionsVM = new FilterOptionsVM(); var result = _context.Products.Where(p => p.HasDeleted == false) .Include(p => p.ProductImages).Include(p => p.Brand) .Include(p => p.Mechanism).Include(p => p.WaterProtection) .Include(p => p.GlassType).Include(p => p.CaseThick) .Include(p => p.BandType).Include(p => p.ProductCategories) .ThenInclude(p => p.Category).AsQueryable(); if (PriceFrom != null) { result = result.Where(x => x.Price >= PriceFrom); optionsVM.PriceFrom = PriceFrom; } if (PriceTo != null) { result = result.Where(x => x.Price <= PriceTo); optionsVM.PriceTo = PriceTo; } if (CategoryId != null) { result = result.Where(x => x.ProductCategories.Select(pc => pc.CategoryId == CategoryId).FirstOrDefault()); optionsVM.CategoryId = CategoryId; } if (BrandId != null) { result = result.Where(x => x.BrandId == BrandId); optionsVM.BrandId = BrandId; } if (MechanismId != null) { result = result.Where(x => x.MechanismId == MechanismId); optionsVM.MechanismId = MechanismId; } if (WaterProtectionId != null) { result = result.Where(x => x.WaterProtectionId == WaterProtectionId); optionsVM.WaterProtectionId = WaterProtectionId; } if (BandTypeId != null) { result = result.Where(x => x.BandTypeId == BandTypeId); } if (CaseThickId != null) { result = result.Where(x => x.CaseThickId == CaseThickId); optionsVM.CaseThicksId = CaseThickId; } if (GlassTypeId != null) { result = result.Where(x => x.GlassTypeId == GlassTypeId); optionsVM.GlassTypeId = GlassTypeId; } ProductPartialVM model = new ProductPartialVM(); List <BasketVM> filteredProducts = new List <BasketVM>(); foreach (Product item in result) { BasketVM basketVM = new BasketVM { Id = item.Id, Model = item.Model, WatchCode = item.WatchCode, Price = item.Price, Image = item.Image, Discount = item.Discount }; filteredProducts.Add(basketVM); } optionsVM.BandTypeId = BandTypeId; Response.Cookies.Append("filterOptions", JsonConvert.SerializeObject(optionsVM)); Response.Cookies.Append("filter", JsonConvert.SerializeObject(filteredProducts)); return(RedirectToAction("Index", "Product")); }