public ActionResult CreateCategory() { using (var db = new WebStoreModel()) { var viewModel = new CreateCategoryViewModel(); return(View(viewModel)); } }
public ActionResult CreateProduct() { using (var db = new WebStoreModel()) { var viewModel = new ProductCreateProductViewModel(); viewModel.Categories = db.ProductCategories.ToList(); return(View(viewModel)); } }
public ActionResult DeleteCategory(int id) { using (var db = new WebStoreModel()) { var category = db.ProductCategories.FirstOrDefault(c => c.CategoryId == id); db.ProductCategories.RemoveRange(new[] { category }); db.SaveChanges(); return(RedirectToAction("Index")); } }
public ActionResult DeleteProduct(int id) { using (var db = new WebStoreModel()) { var product = db.Products.FirstOrDefault(p => p.ProductId == id); db.Products.RemoveRange(new[] { product }); db.SaveChanges(); return(RedirectToAction("Products")); } }
public ActionResult EditCategory(int id) { using (var db = new WebStoreModel()) { var category = db.ProductCategories.FirstOrDefault(c => c.CategoryId == id); var model = new EditCategoryViewModel { Name = category.Name, CategoryId = category.CategoryId }; return(View(model)); } }
// visa detaljer om vald produkt public ActionResult View(int id) { using (var db = new WebStoreModel()) { var product = db.Products.FirstOrDefault(p => p.ProductId == id); var viewModel = new ProductViewViewModel { Name = product.Name, Description = product.Description, Price = product.Price, }; return(View(viewModel)); } }
// visar alla kategorier public ActionResult Index() { var viewModel = new ProductIndexViewModel(); using (var db = new WebStoreModel()) { viewModel.Categories.AddRange(db.ProductCategories.Select(pc => new ProductIndexViewModel.ProductCategoryListViewModel { CategoryId = pc.CategoryId, Name = pc.Name, })); } return(View(viewModel)); }
// visar sökresultaten public ActionResult SearchResult() { var viewModel = new ProductIndexViewModel(); using (var db = new WebStoreModel()) { viewModel.Products.AddRange(db.Products.Select(p => new ProductIndexViewModel.ProductListViewModel { Name = p.Name, Description = p.Description, Price = p.Price, })); } return(View(viewModel)); }
public ActionResult EditCategory(EditCategoryViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var category = db.ProductCategories.FirstOrDefault(c => c.CategoryId == model.CategoryId); category.Name = model.Name; category.CategoryId = model.CategoryId; db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult EditProduct(int id) { using (var db = new WebStoreModel()) { var product = db.Products.FirstOrDefault(p => p.ProductId == id); var viewModel = new ProductEditViewModel { Name = product.Name, Description = product.Description, Price = product.Price, ProductId = product.ProductId, Categories = db.ProductCategories.ToList() }; return(View(viewModel)); } }
public ActionResult CreateCategory(CreateCategoryViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var category = new ProductCategory { Name = model.Name }; db.ProductCategories.Add(category); db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult EditProduct(ProductEditViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var product = db.Products.FirstOrDefault(p => p.ProductId == model.ProductId); product.Name = model.Name; product.Description = model.Description; product.Price = model.Price; product.CategoryId = model.SelectedCategoryId; db.SaveChanges(); } return(RedirectToAction("Products")); }
public ActionResult Search(string SearchProduct, string SearchDescription) { using (var db = new WebStoreModel()) { var viewModel = new ProductIndexViewModel { SearchProduct = SearchProduct, SearchDescription = SearchDescription }; viewModel.Products.AddRange(db.Products.Select(p => new ProductIndexViewModel.ProductListViewModel { Name = p.Name, Description = p.Description, Price = p.Price, ProductId = p.ProductId, }).ToList().Where(x => Matches(x, SearchProduct, SearchDescription))); return(View("SearchResult", viewModel)); } }
public ActionResult CreateProduct(ProductCreateProductViewModel model) { if (!ModelState.IsValid) { return(View(model)); } using (var db = new WebStoreModel()) { var product = new Product { Name = model.Name, Description = model.Description, Price = model.Price, CategoryId = model.SelectedCategoryId, }; db.Products.Add(product); db.SaveChanges(); } return(RedirectToAction("Products")); }
// visar alla produkter i vald kategori public ActionResult Category(int id, string sort) { using (var db = new WebStoreModel()) { var category = db.ProductCategories.FirstOrDefault(c => c.CategoryId == id); var product = db.Products.FirstOrDefault(p => p.CategoryId == id); var viewModel = new ProductCategoryViewModel { Name = product.Name, Description = product.Description, Price = product.Price, Products = category.Products.ToList(), CategoryName = category.Name, }; if (sort == "NameAsc") { viewModel.Products = viewModel.Products.OrderBy(p => p.Name).ToList(); } else if (sort == "NameDesc") { viewModel.Products = viewModel.Products.OrderByDescending(p => p.Name).ToList(); } if (sort == "PriceAsc") { viewModel.Products = viewModel.Products.OrderBy(p => p.Price).ToList(); } else if (sort == "PriceDesc") { viewModel.Products = viewModel.Products.OrderByDescending(p => p.Price).ToList(); } viewModel.CurrentSort = sort; return(View(viewModel)); } }
// visar alla produkter public ActionResult Products(string sort) { var viewModel = new ProductIndexViewModel(); using (var db = new WebStoreModel()) { viewModel.Products.AddRange(db.Products.Select(p => new ProductIndexViewModel.ProductListViewModel { ProductId = p.ProductId, Name = p.Name, Description = p.Description, Price = p.Price, })); } if (sort == "NameAsc") { viewModel.Products = viewModel.Products.OrderBy(p => p.Name).ToList(); } else if (sort == "NameDesc") { viewModel.Products = viewModel.Products.OrderByDescending(p => p.Name).ToList(); } if (sort == "PriceAsc") { viewModel.Products = viewModel.Products.OrderBy(p => p.Price).ToList(); } else if (sort == "PriceDesc") { viewModel.Products = viewModel.Products.OrderByDescending(p => p.Price).ToList(); } viewModel.CurrentSort = sort; return(View(viewModel)); }