private void Search()
        {
            Page = 1;

            _products = _db.Products
                        .Include(x => x.Categories)
                        .ToList();

            if (ChosenCategories.Count > 0)
            {
                _products = _products
                            .Where(x => x.Categories.Any(y => ChosenCategories.Contains(y)))
                            .ToList();
            }

            if (!string.IsNullOrWhiteSpace(Query))
            {
                _products = _products.Where(x => x.Name.ToLower().
                                            Contains(Query.ToLower()) ||
                                            x.Description.ToLower().Contains(Query.ToLower()))
                            .ToList();
            }

            RefreshProducts(_products);
        }
Beispiel #2
0
        public ActionResult SelectCategory(string dropdownMenu)
        {
            var ctx          = new BlogDbContext();
            var mySecondList = ctx.Categories.ToList();

            ViewBag.categories = mySecondList;
            var catName          = ctx.Categories.FirstOrDefault(p => p.Name == dropdownMenu);
            var userId           = User.Identity.GetUserId();
            var chosenCategories = new ChosenCategories
            {
                Name      = catName.Name,
                ProfileID = userId,
            };

            ctx.SelectedCategories.Add(chosenCategories);
            ctx.SaveChanges();

            return(RedirectToAction("Index", "Profile"));
        }
        private void QueryByCategories(string categoryName)
        {
            Page = 1;
            var alreadyInCategories = ChosenCategories
                                      .SingleOrDefault(x => x.Name == categoryName);

            if (alreadyInCategories == null)
            {
                var category = _db.Categories
                               .SingleOrDefault(x => x.Name == categoryName);

                ChosenCategories.Add(category);
            }
            else
            {
                ChosenCategories.Remove(alreadyInCategories);
            }


            Search();
        }