Ejemplo n.º 1
0
        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"));
            }
        }
Ejemplo n.º 2
0
        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"));
            }
        }
Ejemplo n.º 3
0
        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"));
        }
Ejemplo n.º 4
0
        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"));
        }
Ejemplo n.º 5
0
        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"));
        }
Ejemplo n.º 6
0
        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"));
        }