Ejemplo n.º 1
0
        public async Task<ActionResult> Edit(EditProductViewModel model, IEnumerable<HttpPostedFileBase> uploads, string[] selectedSizes)
        {
            Product prod = db.Products.Include(i => i.Sizes).Include(p => p.FilePaths).Where(p => p.ProductId == model.Id).Single();
            List<FilePath> actualImages = db.FilePaths.Where(p => p.ProductId == model.Id).ToList();

            if (ModelState.IsValid)
            {
                // check files upload
                // if exists 
                // delete old images and add new to product images list,
                // save on server
                if (uploads != null)
                {
                    foreach (FilePath file in actualImages)
                    {
                        string filePath = Request.MapPath("~/Content/Images/" + file.FileName);
                        System.IO.File.Delete(filePath);
                    }
                    db.FilePaths.RemoveRange(actualImages);


                    //prod.FilePaths = new List<FilePath>();

                    foreach (HttpPostedFileBase upload in uploads)
                    {
                        if (upload != null && upload.ContentLength > 0)
                        {
                            FilePath photo = new FilePath
                            {
                                FileName = Guid.NewGuid().ToString() + Path.GetExtension(upload.FileName),
                                FileType = FileType.Photo
                            };
                            prod.FilePaths.Add(photo);
                            upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images"), photo.FileName));
                        }
                    }
                }

                //update sizes
                UpdateProductSizes(selectedSizes, prod);

                // calculate product price
                if (prod.Discount > 0)
                {
                    prod.Price = model.NormalPrice - model.NormalPrice * ((decimal)model.Discount / 100);
                }
                else
                {
                    prod.Price = model.NormalPrice;
                }

                //attach model properties as product properties
                prod.ProductId = model.Id;
                prod.ProductName = model.ProductName;
                prod.Desc = model.Desc;
                prod.Material = model.Material;
                prod.NormalPrice = model.NormalPrice;
                prod.Discount = model.Discount;
                prod.CategoryId = model.CategoryId;
                prod.ColorId = model.ColorId;
                prod.EditDate = DateTime.Now;

                //save changes
                db.Entry(prod).State = EntityState.Modified;
                await db.SaveChangesAsync();

                // set confirmation message to Index() View
                TempData["message"] = string.Format("{0} has been edited", prod.ProductName);

                return RedirectToAction("Index");
            }

            // return form if invalid
            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int? id)
        {
            if (id == null)
            { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); }

            Product product = db.Products.Include(p => p.Sizes).Include(p => p.FilePaths).Where(i => i.ProductId == id).Single();

            // set selected checkbox for each product size
            PopulateAssignedSizeData(product);

            if (product == null)
            {
                return HttpNotFound();
            }

            // set default view model values
            EditProductViewModel model = new EditProductViewModel()
            {
                Id = product.ProductId,
                ProductName = product.ProductName,
                Desc = product.Desc,
                Material = product.Material,
                NormalPrice = product.NormalPrice,
                Discount = product.Discount,
                Price = product.Price,
                CategoryId = product.CategoryId,
                ColorId = product.ColorId,
                FilePaths = product.FilePaths.ToList(),
                getCategories = new SelectList(Retriever.GetCategories(), "CategoryId", "CategoryName", product.Category.CategoryName),
                getColors = new SelectList(Retriever.GetColors(), "ColorId", "ColorName", product.Color.ColorName)
            };

            return View(model);
        }