Beispiel #1
0
        public async Task <IActionResult> Index(int productpage = 1, string searchName = null, string searchType = null)
        {
            ProductViewPage productVM = new ProductViewPage()
            {
                Products = new List <Models.Product>(),
            };

            StringBuilder param = new StringBuilder();

            param.Append("/Customer/Shop?productPage=:"); //phân trang


            param.Append("&searchName=");
            if (searchName != null)
            {
                param.Append(searchName);
            }

            param.Append("&searchType=");
            if (searchType != null)
            {
                param.Append(searchType);
            }

            productVM.Products = _db.Products.Include(m => m.ProductTypes).ToList();

            if (searchName != null)
            {
                productVM.Products = await _db.Products.Include(a => a.ProductTypes).Where(a => a.ProductName.ToLower().Contains(searchName.ToLower())).ToListAsync();
            }
            if (searchType != null)
            {
                productVM.Products = await _db.Products.Where(a => a.ProductTypes.ProductTypeName == searchType).ToListAsync();
            }


            var count = productVM.Products.Count;

            productVM.Products = productVM.Products.OrderBy(p => p.ProductId)
                                 .Skip((productpage - 1) * PageSize)
                                 .Take(PageSize).ToList();

            productVM.PagingInfo = new PagingInfo
            {
                CurrentPage  = productpage,
                ItemsPerPage = PageSize,
                TotalItems   = count,
                urlParam     = param.ToString()
            };

            return(View(productVM));
        }
Beispiel #2
0
 public ProductController(ApplicationDbContext db, HostingEnvironment hostingEnvironment)
 {
     _db = db;
     _hostingEnvironment = hostingEnvironment;
     ProductsVM          = new ProductViewModels()
     {
         ProductTypes = _db.ProductTypes.ToList(),
         Products     = new Models.Product(),
     };
     pvp = new ProductViewPage()
     {
         Products = new List <Models.Product>()
     };
 }