Пример #1
0
        public ActionResult Edit([Bind(Include = "ProductId,Name,ProductCategoryId,Price,Quantity,Description,ImagePath,ImageName,IsChangeImage")] ProductViewModel productViewModel)
        {
            if (ModelState.IsValid)
            {
                var imagePath = productViewModel.ImageName;
                if (productViewModel.IsChangeImage)
                {
                    var myHelper = new ControllersHelper();
                    imagePath = myHelper.SaveFile(productViewModel.ImagePath, ModelState, Server);
                }

                Product product = new Product
                {
                    ProductId         = productViewModel.ProductId,
                    Name              = productViewModel.Name,
                    Description       = productViewModel.Description,
                    Price             = productViewModel.Price,
                    Quantity          = productViewModel.Quantity,
                    ProductCategoryId = productViewModel.ProductCategoryId,
                    ImagePath         = imagePath
                };
                db.Entry(product).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            ViewBag.ProductCategoryId = new SelectList(db.ProductCategory, "ProductCategoryId", "Name", productViewModel.ProductCategoryId);
            return(View(productViewModel));
        }
        public ActionResult Create([Bind(Include = "ProductCategoryId,Name,Description,ImagePath")] ProductCategoryViewModel productCategoryViewModel)
        {
            if (ModelState.IsValid)
            {
                var myHelper  = new ControllersHelper();
                var imagePath = myHelper.SaveFile(productCategoryViewModel.ImagePath, ModelState, Server);

                ProductCategory productCategory = new ProductCategory
                {
                    Name        = productCategoryViewModel.Name,
                    Description = productCategoryViewModel.Description,
                    ImagePath   = imagePath
                };
                db.ProductCategory.Add(productCategory);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(productCategoryViewModel));
        }
        public ActionResult Edit([Bind(Include = "ProductCategoryId,Name,Description,ImagePath,ImageName,IsChangeImage")] ProductCategoryViewModel productCategoryViewModel)
        {
            if (ModelState.IsValid)
            {
                var imagePath = productCategoryViewModel.ImageName;
                if (productCategoryViewModel.IsChangeImage)
                {
                    var myHelper = new ControllersHelper();
                    imagePath = myHelper.SaveFile(productCategoryViewModel.ImagePath, ModelState, Server);
                }

                ProductCategory productCategory = new ProductCategory
                {
                    ProductCategoryId = productCategoryViewModel.ProductCategoryId,
                    Name        = productCategoryViewModel.Name,
                    Description = productCategoryViewModel.Description,
                    ImagePath   = imagePath
                };
                db.Entry(productCategory).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(productCategoryViewModel));
        }