public IActionResult Index() { var product = dataContext.Products.Select(p => p).ToList(); var laptop = dataContext.Laptops.Select(p => p).ToList(); var pc = dataContext.PCs.Select(p => p).ToList(); var account = dataContext.Users.Select(p => p).ToList(); List <Product> productLaptop = new List <Product>(); List <Product> productPC = new List <Product>(); List <LaptopCategory> laptopCategories = new List <LaptopCategory>(); List <PCCategory> pcCategories = new List <PCCategory>(); foreach (var item in laptop) { Product newLaptop = dataContext.Products.FirstOrDefault(p => p.ProductId == item.ProductId); productLaptop.Add(newLaptop); LaptopCategory laptopCategory = dataContext.LaptopCategories.FirstOrDefault(p => p.LaptopCategoryId == item.LaptopCategoryId); laptopCategories.Add(laptopCategory); } foreach (var item in pc) { Product newpc = dataContext.Products.FirstOrDefault(p => p.ProductId == item.ProductId); productPC.Add(newpc); PCCategory pcCategory = dataContext.PCCategories.FirstOrDefault(p => p.PCCategoryId == item.PCCategoryId); pcCategories.Add(pcCategory); } ViewBag.Product = product.Count(); ViewBag.Laptop = laptop.Count(); ViewBag.PC = pc.Count(); ViewBag.Account = account.Count(); ViewBag.ListLaptop = laptop; ViewBag.ListPC = pc; return(View()); }
public IActionResult Index(int id) { Product product = dataContext.Products.FirstOrDefault(p => p.ProductId == id); Brand brand = dataContext.Brands.FirstOrDefault(p => p.BrandId == product.BrandId); if (product.CategoryId == 1) { Laptop laptop = dataContext.Laptops.FirstOrDefault(p => p.ProductId == product.ProductId); if (laptop == null) { ViewBag.Status = 1; } else { ViewBag.Laptop = laptop; LaptopCategory laptopCategory = dataContext.LaptopCategories.FirstOrDefault(p => p.LaptopCategoryId == laptop.LaptopCategoryId); ViewBag.Category = laptopCategory.LaptopCategoryName; } } else { PC pc = dataContext.PCs.FirstOrDefault(p => p.ProductId == product.ProductId); if (pc == null) { ViewBag.Status = 1; } else { ViewBag.PC = pc; PCCategory pCCategory = dataContext.PCCategories.FirstOrDefault(p => p.PCCategoryId == pc.PCCategoryId); ViewBag.Category = pCCategory.PCCategoryName; } } return(View(product)); }
public IActionResult Detail(int id) { Laptop laptop = dataContext.Laptops.FirstOrDefault(p => p.LaptopId == id); Product product = dataContext.Products.FirstOrDefault(p => p.ProductId == laptop.ProductId); LaptopCategory laptopCategory = dataContext.LaptopCategories.FirstOrDefault(p => p.LaptopCategoryId == laptop.LaptopCategoryId); Brand brand = dataContext.Brands.FirstOrDefault(p => p.BrandId == product.BrandId); ViewBag.product = product.ProductName; ViewBag.brand = brand.BrandName; ViewBag.category = laptopCategory.LaptopCategoryName; return(View(laptop)); }
public IActionResult Category(int id) { if (id == 0) { return(Redirect("/Category")); } LaptopCategory theCategory = context.Categories.Include(cat => cat.Laptops).Single(cat => cat.ID == id); ViewBag.title = "Laptops in category: " + theCategory.Name; return(View("Index", theCategory.Laptops)); }
public IActionResult Add(AddCategoryViewModel addCategoryViewModel) { if (ModelState.IsValid) { LaptopCategory newCategory = new LaptopCategory { Name = addCategoryViewModel.Name }; context.Categories.Add(newCategory); context.SaveChanges(); return(Redirect("/Category")); } return(View(addCategoryViewModel)); }
public IActionResult Index(string search, string name = null) { var query = dataContext.Laptops.Where(p => p.LaptopCategory.LaptopCategoryName != null).ToList(); List <LaptopCategory> laptopCategories = new List <LaptopCategory>(); List <Product> laptop = new List <Product>(); foreach (var item in query) { LaptopCategory laptopCategory = dataContext.LaptopCategories.FirstOrDefault(p => p.LaptopCategoryId == item.LaptopCategoryId); laptopCategories.Add(laptopCategory); Product product = dataContext.Products.FirstOrDefault(p => p.ProductId == item.ProductId); laptop.Add(product); } var categoryList = dataContext.LaptopCategories.ToList(); ViewBag.Category = categoryList; var queryName = dataContext.Laptops.Where(s => s.Product.ProductName.Contains(search)).ToList(); if (!string.IsNullOrEmpty(search) && name == "all") { return(View(queryName)); } else if (name != null && !string.IsNullOrEmpty(search)) { var queryNameInCategory = dataContext.Laptops.Where(s => s.LaptopCategory.LaptopCategoryName == name && s.Product.ProductName.Contains(search)).ToList(); return(View(queryNameInCategory)); } else if (name == null && string.IsNullOrEmpty(search)) { return(View(query)); } else if (string.IsNullOrEmpty(search) && name != "all") { var queryCategory = dataContext.Laptops.Where(s => s.LaptopCategory.LaptopCategoryName == name).ToList(); return(View(queryCategory)); } return(View(query)); }
public IActionResult Add(AddLaptopViewModel addLaptopViewModel) { if (ModelState.IsValid) { LaptopCategory newLaptopCategory = context.Categories.Single(c => c.ID == addLaptopViewModel.CategoryID); // Add new laptop to the list of existing laptops Laptop newLaptop = new Laptop { Name = addLaptopViewModel.Name, Description = addLaptopViewModel.Description, Category = newLaptopCategory }; context.Laptops.Add(newLaptop); context.SaveChanges(); return(Redirect("/Laptop")); } return(View(addLaptopViewModel)); }
protected override bool Save() { bool result = true; if (this.Entity.IsValid) { LaptopCategory pc = new LaptopCategory(); pc.Category = this.Category; pc.Laptop = this.Entity; (RepositoryManager.GetRepository(typeof(LaptopCategory)) as Repository <LaptopCategory>).AddEntity(pc); RepositoryManager.GetRepository(typeof(LaptopCategory)).SaveToDatabase(); } else { result = false; MessageBox.Show("One or more fields are invalid. The product category could not be saved."); } return(result); }