示例#1
0
 public ActionResult Edit(ProductMG model)
 {
     if (ModelState.IsValid)
     {
         var productImage = productMGRepository.Get(model.ProductID);
         if (productImage != null)
         {
             productImage.ImgPath       = model.ImgPath;
             productImage.IsDefaultImg  = model.IsDefaultImg;
             productImage.ThumbnailPath = model.ThumbnailPath;
             productImage.DisplayOrder  = model.DisplayOrder;
         }
         productMGRepository.Update(productImage);
         return(RedirectToAction("index"));
     }
     return(View(model));
 }
示例#2
0
        public ActionResult Edit(ProductEditViewModel model)
        {
            if (ModelState.IsValid)
            {
                var product   = productRepository.Get(model.ID);
                var productMG = productMGRepository.Get(model.ID);
                if (product != null)
                {
                    product.Name          = model.Name;
                    product.Brand         = model.Brand;
                    product.CategoryID    = model.CategoryID;
                    product.Details       = model.Details;
                    product.IsActive      = model.IsActive;
                    product.IsFavorite    = model.IsFavorite;
                    product.Price         = model.Price;
                    product.StockQuantity = model.StockQuantity;

                    if (productMG != null)
                    {
                        string uniqueFileName = null;
                        int    i = 1;
                        if (model.ImagePath[0] != null && model.ImagePath.Count > 0)
                        {
                            if (model.ExistenceImagePath != null)
                            {
                                string photoPath = Path.Combine(Server.MapPath("~/ProductPhoto"), model.ExistenceImagePath);
                                System.IO.File.Delete(photoPath);
                            }
                            foreach (var photo in model.ImagePath)
                            {
                                uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                                string uploaddFile = Path.Combine(Server.MapPath("~/ProductPhoto"), uniqueFileName);
                                photo.SaveAs(uploaddFile);
                                productMG.ImgPath = uniqueFileName;
                                productMGRepository.Update(productMG);
                                i++;
                            }
                        }
                    }
                    productRepository.Update(product);
                    return(RedirectToAction("Index"));
                }
                return(View(model));
            }
            return(View(model));
        }
示例#3
0
        public ActionResult Edit(VMProductInfo product, HttpPostedFileBase Image, HttpPostedFileBase Thumbnail)
        {
            var img   = imgdb.Get(product.ID);
            var path1 = img.ImgPath;
            var path2 = img.ThumbnailPath;

            if (ModelState.IsValid)
            {
                var allowedExtensions = new[] { ".jpg", ".png", ".jpg", "jpeg" };
                if (Image != null)
                {
                    var ext1 = Path.GetExtension(Image.FileName).ToLower();
                    if (allowedExtensions.Contains(ext1))
                    {
                        var fileName1 = Path.GetFileName(Image.FileName);
                        path1 = Path.Combine(Server.MapPath("~/Content/Images/ProductsImg"), fileName1);

                        if (System.IO.File.Exists(img.ImgPath))
                        {
                            System.IO.File.Delete(img.ImgPath);
                        }

                        Image.SaveAs(path1);
                    }
                }
                else
                {
                    ViewBag.Error = "Please choose only Image file";
                }
                if (Thumbnail != null)
                {
                    var ext2 = Path.GetExtension(Thumbnail.FileName).ToLower();
                    if (allowedExtensions.Contains(ext2))
                    {
                        var fileName2 = Path.GetFileName(Thumbnail.FileName);
                        path2 = Path.Combine(Server.MapPath("~/Content/Images/ProductsThum"), fileName2);


                        if (System.IO.File.Exists(img.ThumbnailPath))
                        {
                            System.IO.File.Delete(img.ThumbnailPath);
                        }
                        Image.SaveAs(path2);
                    }
                }
                else
                {
                    ViewBag.Error = "Please choose only Image file";
                }

                img.ImgPath       = path1;
                img.ThumbnailPath = path2;
                imgdb.Update(img);
                db.Update(product);

                return(RedirectToAction("Index"));
            }

            ViewBag.Thum       = Path.GetFileName(img.ThumbnailPath);
            ViewBag.Img        = Path.GetFileName(img.ImgPath);
            ViewBag.CategoryID = new SelectList(catdb.GetAll().ToList(), "ID", "Name", product.CategoryID);
            return(View(product));
        }