示例#1
0
 public ActionResult Create(ProductViewModel model, int?IsDefaultCheckBox)
 {
     if (ModelState.IsValid)
     {
         try
         {
             Product product = new Product
             {
                 Name          = model.Name,
                 Brand         = model.Brand,
                 CategoryID    = model.CategoryID,
                 Details       = model.Details,
                 IsActive      = model.IsActive,
                 IsFavorite    = model.IsFavorite,
                 Price         = model.Price,
                 StockQuantity = model.StockQuantity
             };
             productRepository.Add(product);
             string uniqueFileName = null;
             int    i = 1;
             if (model.ImagePath != null && model.ImagePath.Count > 0)
             {
                 foreach (var photo in model.ImagePath)
                 {
                     uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                     string uploaddFile = Path.Combine(Server.MapPath("~/ProductPhoto"), uniqueFileName);
                     photo.SaveAs(uploaddFile);
                     ProductMG productMG = new ProductMG
                     {
                         ImgPath       = uniqueFileName,
                         DisplayOrder  = i,
                         ProductID     = product.ID,
                         ThumbnailPath = ""
                     };
                     if (i == IsDefaultCheckBox)
                     {
                         productMG.IsDefaultImg = true;
                     }
                     productMGRepository.Add(productMG);
                     i++;
                 }
             }
         }
         catch (Exception ex)
         {
             ModelState.AddModelError("", ex.Message);
         }
         return(RedirectToAction("Index"));
     }
     return(View(model));
 }
示例#2
0
        public ActionResult Create(VMProductInfo product, HttpPostedFileBase Image, HttpPostedFileBase Thumbnail)
        {
            var path1 = "~/Content/Images/ProductsImg";
            var path2 = "~/Content/Images/ProductsThum";
            var InvalideFileFormate = "";

            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(path1), fileName1);

                        Image.SaveAs(path1);
                    }
                    else
                    {
                        InvalideFileFormate = "Please choose only Image file";
                        ViewBag.Error       = InvalideFileFormate;
                        ViewBag.CategoryID  = new SelectList(catdb.GetAll().ToList(), "ID", "Name", product.CategoryID);
                        return(View(product));
                    }
                }

                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(path2), fileName2);

                        Thumbnail.SaveAs(path2);
                    }
                    else
                    {
                        InvalideFileFormate = "Please choose only  Image file";
                        ViewBag.Error       = InvalideFileFormate;
                        ViewBag.CategoryID  = new SelectList(catdb.GetAll().ToList(), "ID", "Name", product.CategoryID);
                        return(View(product));
                    }
                }

                var         newProduct = db.Add(product);
                VMProductMG img        = new VMProductMG();
                img.ProductID     = newProduct.ID;
                img.ImgPath       = path1;
                img.ThumbnailPath = path2;
                imgdb.Add(img);


                return(RedirectToAction("Index"));
            }

            ViewBag.CategoryID = new SelectList(catdb.GetAll().ToList(), "ID", "Name", product.CategoryID);
            return(View(product));
        }