public ActionResult Edit([Bind(Include = "ID,Name,Price,Description,Inventory,Created,Modified")] Product product, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                product.Modified = DateTime.UtcNow;

                db.Entry(product).State = EntityState.Modified;
                //TODO: Figure out why/fix the NullReferenceException thrown when editing products
                string fileName = image.FileName;

                if (ConfigurationManager.AppSettings["UseLocalStorage"] == "true")
                {
                    int i = 1;
                    while (System.IO.File.Exists(Server.MapPath("/content/images/" + fileName)))
                    {
                        fileName = System.IO.Path.GetFileNameWithoutExtension(fileName) + i.ToString() + System.IO.Path.GetExtension(fileName);
                        i++;
                    }
                    image.SaveAs(Server.MapPath("/content/images/" + fileName));
                    fileName = "/content/images/" + fileName;
                }
                else
                {
                    CloudStorageAccount account = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]);
                    var blobClient    = account.CreateCloudBlobClient();
                    var rootContainer = blobClient.GetRootContainerReference();
                    rootContainer.CreateIfNotExists();
                    rootContainer.SetPermissions(new BlobContainerPermissions {
                        PublicAccess = BlobContainerPublicAccessType.Blob
                    });
                    var blob = rootContainer.GetBlockBlobReference(fileName);
                    blob.UploadFromStream(image.InputStream);

                    fileName = blob.Uri.ToString();
                }

                if (db.ProductImages.Any(x => x.ID == product.ID))
                {
                    ProductImage pi = db.ProductImages.FirstOrDefault(x => x.ProductID == product.ID);
                    pi.FilePath = fileName;
                    pi.Modified = DateTime.UtcNow;
                }
                else
                {
                    product.ProductImages.Add(new ProductImage
                    {
                        FilePath = fileName,
                        Created  = DateTime.UtcNow,
                        Modified = DateTime.UtcNow
                    });
                }

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(View(product));
        }
 public ActionResult Edit([Bind(Include = "ID,Name,Description,Created,Modified")] Category category)
 {
     if (ModelState.IsValid)
     {
         db.Entry(category).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(category));
 }
Beispiel #3
0
 public ActionResult Edit([Bind(Include = "ID,ProductID,Email,Rating,Body,Created,Modified")] Review review)
 {
     if (ModelState.IsValid)
     {
         db.Entry(review).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.ProductID = new SelectList(db.Products, "ID", "Name", review.ProductID);
     return(View(review));
 }
 public ActionResult Edit([Bind(Include = "CategoryID,ProductID,Created,Modified")] CategoriesProduct categoriesProduct)
 {
     if (ModelState.IsValid)
     {
         db.Entry(categoriesProduct).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.CategoryID = new SelectList(db.Categories, "ID", "Name", categoriesProduct.CategoryID);
     ViewBag.ProductID  = new SelectList(db.Products, "ID", "Name", categoriesProduct.ProductID);
     return(View(categoriesProduct));
 }