// GET: Categories public async Task <IActionResult> Index(int?id, int?productID) { var viewModel = new CategoryIndexData(); viewModel.Categories = await _context.Categories .Include(i => i.PublishedProducts) .ThenInclude(i => i.Product) .ThenInclude(i => i.Orders) .ThenInclude(i => i.Customer) .AsNoTracking() .OrderBy(i => i.CategoryName) .ToListAsync(); if (id != null) { ViewData["CategoryID"] = id.Value; Category category = viewModel.Categories.Where(i => i.ID == id.Value).Single(); viewModel.Products = category.PublishedProducts.Select(s => s.Product); } if (productID != null) { ViewData["ProductID"] = productID.Value; viewModel.Orders = viewModel.Products.Where( x => x.ID == productID).Single().Orders; } return(View(viewModel)); }
public ActionResult Index(int?id, int?subcatID, int?postID) { var viewModel = new CategoryIndexData(); viewModel.Categories = db.Categories.OrderBy(i => i.Name); if (id != null) { ViewBag.ID = id.Value; viewModel.SubCategories = viewModel.Categories.FirstOrDefault(c => c.ID == id).SubCategory; } if (subcatID != null) { ViewBag.SubCategoryID = subcatID.Value; viewModel.Posts = db.Posts.Include(p => p.FilesPost).Where(x => x.SubCategoryID == subcatID); } return(View(viewModel)); }
// GET: Categories public async Task <IActionResult> Index(int?id, string sortOrder, string searchString) { var viewModel = new CategoryIndexData(); viewModel.Categories = await _context.Categories .Include(i => i.Products) // viewModel.Categories = await _context.Categories // .Include(i => i.Products) // .ThenInclude(i => i.InputDetails) // .Include(i => i.Products) // .ThenInclude(i => i.SaleDetails) // .Include(i => i.Provider) // .ThenInclude(i => i.Inputs) .AsNoTracking() .OrderBy(i => i.CategoryID) .ToListAsync(); //Categories Sort Order begin ViewData["CurrentSort"] = sortOrder; ViewData["NameSortParm"] = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewData["DateSortParm"] = sortOrder == "Date" ? "date_desc" : "Date"; ViewData["CurrentFilter"] = searchString; switch (sortOrder) { case "name_desc": viewModel.Categories = viewModel.Categories.OrderByDescending(s => s.CategoryName); break; case "Date": viewModel.Categories = viewModel.Categories.OrderBy(s => s.CategoryID); break; case "date_desc": viewModel.Categories = viewModel.Categories.OrderByDescending(s => s.CategoryID); break; default: viewModel.Categories = viewModel.Categories.OrderBy(s => s.CategoryName); break; } //Categories Sort Order End //Categories Search //Categories detail selected if (id != null) { ViewData["CategoryID"] = id.Value; viewModel.Categories = await _context.Categories .Include(i => i.Products) .OrderBy(i => i.CategoryID) .ToListAsync(); Category category = viewModel.Categories .Where(c => c.CategoryID == id.Value) .Single(); viewModel.Products = category.Products.ToList(); } if (!String.IsNullOrEmpty(searchString)) { var category = from s in _context.Categories select s; category = category.Where(s => s.Description.Contains(searchString) || s.CategoryName.Contains(searchString) //|| s.CategoryName.ToUpper().Contains(searchString.ToUpper()) ); viewModel.Categories = category.ToList(); } return(View(viewModel)); // return View(await category.ToListAsync()); //return View(await _context.Categories.ToListAsync()); }