Exemplo n.º 1
0
        public async Task <ActionResult> ProductAdd(ShopProductVM model)
        {
            if (ModelState.IsValid)
            {
                using (Db db = new Db())
                {
                    if (db.ShopProducts.Any(m => m.Name == model.Name))
                    {
                        ModelState.AddModelError("", "This name product alredy exist");
                        return(View(model));
                    }

                    ShopProductDB productAdd = new ShopProductDB
                    {
                        Name             = model.Name,
                        Description      = model.Description,
                        Price            = model.Price,
                        ShopCategoryId   = model.ShopCategoryId,
                        ShopCategoryName = db.ShopCategories.Find(model.ShopCategoryId).Name
                    };

                    db.ShopProducts.Add(productAdd);
                    await db.SaveChangesAsync();
                }
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public IActionResult Shop()
        {
            ShopProductVM shopProductVM = new ShopProductVM
            {
                shopProducts = _db.shopProducts,
            };

            return(View(shopProductVM));
        }
Exemplo n.º 3
0
        public ActionResult ProductAdd()
        {
            ViewBag.Menu = "Shop";

            ShopProductVM product = new ShopProductVM();

            using (Db db = new Db())
            {
                product.ShopCategories = new SelectList(db.ShopCategories.ToList(), "Id", "Name");
            }
            return(View(product));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> ProductDelete(ShopProductVM model)
        {
            using (Db db = new Db())
            {
                ShopProductDB productDb = await db.ShopProducts.FindAsync(model.Id);

                if (productDb != null)
                {
                    db.ShopProducts.Remove(productDb);
                    db.SaveChanges();

                    if (!db.ShopProducts.Any(m => m.Id == model.Id))
                    {
                    }
                }
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 5
0
        public async Task <ActionResult> ProductEdit(int?id)
        {
            ViewBag.Menu = "Shop";
            if (id == null)
            {
                return(RedirectToAction("Index"));
            }
            using (Db db = new Db())
            {
                ShopProductVM product = new ShopProductVM(await db.ShopProducts.FindAsync((int)id));

                if (product != null)
                {
                    product.ShopCategories = new SelectList(db.ShopCategories.ToList(), "Id", "Name");
                    return(View(product));
                }
            }
            return(RedirectToAction("Index"));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> ProductEdit(ShopProductVM model)
        {
            using (Db db = new Db())
            {
                if (ModelState.IsValid)
                {
                    ShopProductDB productDb = await db.ShopProducts.FindAsync(model.Id);

                    if (productDb != null)
                    {
                        if (db.ShopProducts.Where(m => m.Id != model.Id).Any(m => m.Name == model.Name))
                        {
                            ModelState.AddModelError("", "This name product already exist");
                            return(View(model));
                        }
                        productDb.Name        = model.Name;
                        productDb.Price       = model.Price;
                        productDb.ImageName   = model.ImageName;
                        productDb.Description = model.Description;

                        ShopCategoryDB category = await db.ShopCategories.FindAsync(model.ShopCategoryId);

                        if (category != null)
                        {
                            productDb.ShopCategoryId   = category.Id;
                            productDb.ShopCategoryName = category.Name;
                        }
                        else
                        {
                            return(View(model.Id));
                        }

                        db.Entry(productDb).State = System.Data.Entity.EntityState.Modified;
                        await db.SaveChangesAsync();

                        return(RedirectToAction("Index"));
                    }
                }
                return(View(model.Id));
            }
        }