Beispiel #1
0
        public async Task <ActionResult> SearchAsync(string searchString)
        {
            var result = new List <ProductHomepageViewModel>();

            if (String.IsNullOrEmpty(searchString))
            {
                return(RedirectToAction("Index", "Home"));
            }
            else
            {
                var search   = CommonHelper.SafePlainText(searchString);
                var products = await _productHomepageQueries.GetProductAsync(lang : CurrentLanguage);

                if (products == null)
                {
                    return(Redirect("/error"));
                }
                result = products.Where(x => x.Name.ToUpper().Contains(search.ToUpper())).ToList();
                if (result.Count() == 0)
                {
                    ViewBag.Notification = "Không tìm thấy kết quả.";
                }
            }


            return(View(result));
        }
Beispiel #2
0
        public async Task <IActionResult> Index(string cateId, int page = 1, int pageSize = PAGE_SIZE_DEFAULT, string sortby = "")
        {
            if (page < 1)
            {
                page = 1;
            }
            ViewData["categories"] = await _categoryQueries.Gets(CurrentLanguage);

            IEnumerable <ProductHomepageViewModel> products = null;

            if (string.IsNullOrWhiteSpace(cateId))
            {
                ViewData["CateID"] = "";
                products           = await _productHomepageQueries.GetProductAsync(lang : CurrentLanguage);

                if (products == null)
                {
                    return(Redirect("/error"));
                }
            }
            else
            {
                ViewData["CateID"] = cateId;
                IEnumerable <Category> categories = await _categoryQueries.Gets(CurrentLanguage);

                products = await GetProductByCategoryAsync(int.Parse(cateId), categories);

                if (products == null)
                {
                    return(Redirect("/error"));
                }
            }

            ViewData["TotalProduct"] = products.Count();
            ViewData["TotalPage"]    = (int)Math.Ceiling(decimal.Divide(products.Count(), pageSize));
            ViewData["currentPage"]  = page;
            ViewData["pagesize"]     = pageSize;
            ViewData["sortby"]       = sortby;

            switch (sortby)
            {
            case "price-low-to-high":
                products = products.OrderBy(x => x.SellingCurrentPrice);
                break;

            case "price-high-to-low":
                products = products.OrderByDescending(x => x.SellingCurrentPrice);
                break;

            case "product-name":
            default:
                products = products.OrderBy(x => x.Name).ToList();
                break;
            }
            products = products.Skip((page - 1) * pageSize).Take(pageSize).ToList();
            return(View(products));
        }