// GET: ProductTypes public async Task <IActionResult> Index() { var model = new ProductTypesViewModel(); // Get line items grouped by product id, including count var counter = from product in _context.Product group product by product.ProductTypeId into grouped select new { grouped.Key, myCount = grouped.Count() }; // Build list of Product instances for display in view model.ProductTypes = await( from t in _context.ProductType join p in _context.Product on t.ProductTypeId equals p.ProductTypeId group new { t, p } by new { t.ProductTypeId, t.Label } into grouped select new ProductType { ProductTypeId = grouped.Key.ProductTypeId, Label = grouped.Key.Label, Quantity = grouped.Select(x => x.p.ProductId).Count(), Products = grouped.Select(x => x.p).Take(3) }).ToListAsync(); return(View(model)); }
public ProductTypesController(ApplicationDbContext db) { _db = db; ProductTypesVM = new ProductTypesViewModel() { Categories = _db.Categories.ToList(), ProductTypes = new Models.ProductTypes() }; }
//Method: Purpose is to render the ProductTypes view, which displays all product categories public async Task <IActionResult> Types() { //This creates a new instance of the ProductTypesViewModel and passes in the current session with the database (context) as an argument ProductTypesViewModel model = new ProductTypesViewModel(context); model.ProductTypes = await context.ProductType.OrderBy(s => s.Label).ToListAsync(); model.ProductTypeSubCategories = await context.ProductTypeSubCategory.OrderBy(s => s.Name).ToListAsync(); //list of subcategories var subCats = context.ProductTypeSubCategory.ToList(); //cycle through each subcategory and define its Quantity as subCats.ForEach(sc => sc.Quantity = context.Product.Count(p => p.ProductTypeSubCategoryId == sc.ProductTypeSubCategoryId)); return(View(model)); }
public async Task <IActionResult> Types() { ProductTypesViewModel vm = new ProductTypesViewModel(); //list of product types var productTypes = _context.ProductTypes //include the products .Include(p => p.Products).ToList(); //I have alist of product types, need to convert to grouped list var groupedProducts = new List <GroupedProducts>(); foreach (ProductType p in productTypes) { groupedProducts.Add(new GroupedProducts() { TypeName = p.Name, ProductCount = p.Products.Count(), Products = p.Products.Take(3).ToList() }); } vm.GroupedProducts = groupedProducts; return(View(vm)); }
private static ProductTypesViewModel CreateProductTypesViewModel(IServiceProvider services) { return(ProductTypesViewModel.LoadViewModel( services.GetRequiredService <IProductTypeCollectionService>())); }