public async Task <IActionResult> ProductTypeList(int id) { var viewModel = new ProductTypeListViewModel(); viewModel.ProductTypeId = id; var productType = await _context.ProductType .Include(pt => pt.Products) .Where(pt => pt.ProductTypeId == id).SingleAsync(); viewModel.ProductType = productType; viewModel.Label = productType.Label; viewModel.Products = productType.Products; viewModel.ProductCount = viewModel.Products.Count(); return(View(viewModel)); }
/* * Author: Ricky Bruner * Purpose: Construct a viewmodel to house the necessary data needed to display the appropriate data for the ProductType Index View. */ // GET: ProductTypes public async Task <IActionResult> Index() { var model = new ProductTypeListViewModel(); // Build list of Product instances for display in view // LINQ is awesome model.GroupedProducts = 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 GroupedProducts { TypeId = grouped.Key.ProductTypeId, TypeName = grouped.Key.Label, ProductCount = grouped.Select(x => x.p.ProductId).Count(), Products = grouped.Select(x => x.p).Take(3) }).ToListAsync(); return(View(model)); }