public virtual ActionResult Index(int?categoryId, ProductSearchModel search, int?page) { var pageNumber = page ?? 1; ViewBag.Search = search; ViewBag.CategoryId = categoryId; var categories = _unitOfWork.Categories.GetAll().ToList(); var productPageVm = new ProductPageVM { Product = _productService.Search(search, categoryId).MapTo <List <ProductVM> >() .ToPagedList(pageNumber, PageSize), Categories = categories, Search = search }; return(View(productPageVm)); }
/// <summary> /// 设备清单第一步 /// </summary> /// <returns></returns> public async Task <IActionResult> AddToList(string keyword, int?page) { ProductPageVM vm = new ProductPageVM() { PageIndex = page == null || page <= 0 ? 1 : page.Value, Keyword = keyword }; var pageSize = 10; var query = _context.Products.AsNoTracking().AsQueryable(); if (!string.IsNullOrEmpty(keyword)) { query = query.Where(d => d.Name.Contains(keyword) || d.SubName.Contains(keyword)); } vm.TotalCount = await query.CountAsync(); var products = await query.OrderByDescending(d => d.Id).ProjectTo <ProductVM>(_mapper.ConfigurationProvider) .Skip((vm.PageIndex - 1) * pageSize).Take(pageSize) .ToListAsync(); foreach (var item in products) { var categories = _context.ProductCategories.Where(d => d.PcategoryProducts.Any(c => c.ProductId == item.Id)).ToList(); item.CategoryTitle = string.Join("、", categories.Select(d => d.Title).ToArray()); } vm.Products = new StaticPagedList <ProductVM>(products, vm.PageIndex, pageSize, vm.TotalCount); ViewData["CartItems"] = _httpContextAccessor.HttpContext.Session.GetObject <List <CartItemVM> >("CartItems"); return(View(vm)); }
public async Task <IActionResult> IndexAsync(int?cid) { var allCategories = await _context.ProductCategories.Where(d => d.Active == true) .OrderByDescending(d => d.Importance).ThenBy(d => d.Id) .ProjectTo <ProductCategoryVM>(_mapper.ConfigurationProvider).ToListAsync(); var vm = new ProductPageVM { Categories = allCategories.Where(d => d.ParentId == null) }; if (cid > 0) { vm.Category = allCategories.FirstOrDefault(d => d.Id == cid); vm.SubCategories = allCategories.Where(d => d.ParentId == cid || d.ParentId == vm.Category.ParentId).Where(d => d.ParentId != null); var ids = vm.SubCategories.Select(d => d.Id).ToList(); ids.Add(cid.Value); if (vm.Category.ParentId == null) { vm.Products = await _context.Products.Where(d => ids.Contains(d.CategoryId) && d.Active == true) .OrderByDescending(d => d.Importance).ThenByDescending(d => d.Id) .ProjectTo <ProductVM>(_mapper.ConfigurationProvider).ToListAsync(); } else { vm.Products = await _context.Products.Where(d => d.CategoryId == cid && d.Active == true) .OrderByDescending(d => d.Importance).ThenByDescending(d => d.Id) .ProjectTo <ProductVM>(_mapper.ConfigurationProvider).ToListAsync(); } vm.CategoryId = cid; } else { vm.Category = vm.Categories.FirstOrDefault(); if (vm.Category != null) { vm.SubCategories = allCategories.Where(d => d.ParentId == vm.Category.Id); var ids = vm.SubCategories.Select(d => d.Id).ToList(); ids.Add(vm.Category.Id); vm.Products = await _context.Products.Where(d => d.CategoryId == vm.Category.Id) .OrderByDescending(d => d.Importance).ThenByDescending(d => d.Id) .ProjectTo <ProductVM>(_mapper.ConfigurationProvider).ToListAsync(); vm.CategoryId = vm.Category.Id; } } if (vm.Category.VideoId > 0) { var video = await _context.Videos.FirstOrDefaultAsync(d => d.Id == vm.Category.VideoId); vm.Video = _mapper.Map <VideoVM>(video); } var url = Request.Path.ToString(); ViewData["PageMeta"] = await _context.PageMetas.FirstOrDefaultAsync(d => d.ObjectId == url && d.ModuleType == (short)ModuleType.MENU); return(View(vm)); }