Ejemplo n.º 1
0
 public ActionResult Create([Bind(Include = "ProductId,ProductTitle,ProductDescription,ProductPrice,CategoryId")] Product product, HttpPostedFileBase upload)
 {
     try {
         if (ModelState.IsValid)
         {
             if (upload != null && upload.ContentLength > 0)
             {
                 var image = new File
                 {
                     FileName = System.IO.Path.GetFileName(upload.FileName),
                     FileType = FileType.Image,
                     ContentType = upload.ContentType
                 };
                 using (var reader = new System.IO.BinaryReader(upload.InputStream))
                 {
                     image.Content = reader.ReadBytes(upload.ContentLength);
                 }
                 product.Files = new List<File> { image };
             }
             db.Products.Add(product);
             db.SaveChanges();
             return RedirectToAction("Index");
         }
     }
     catch (RetryLimitExceededException)
     {
         ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
     }
     ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "CategoryName", product.CategoryId);
     return View(product);
 }
Ejemplo n.º 2
0
        public ActionResult EditPost(int? id, HttpPostedFileBase upload)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var productToUpdate = db.Products.Find(id);
            if (TryUpdateModel(productToUpdate, "",
                new string[] { "ProductTitle", "ProductDescription", "ProductPrice", "CategoryId" }))
            {
                try
                {
                    if (upload != null && upload.ContentLength > 0)
                    {
                        if (productToUpdate.Files.Any(f => f.FileType == FileType.Image))
                        {
                            db.Files.Remove(productToUpdate.Files.First(f => f.FileType == FileType.Image));
                        }
                        var avatar = new File
                        {
                            FileName = System.IO.Path.GetFileName(upload.FileName),
                            FileType = FileType.Image,
                            ContentType = upload.ContentType
                        };
                        using (var reader = new System.IO.BinaryReader(upload.InputStream))
                        {
                            avatar.Content = reader.ReadBytes(upload.ContentLength);
                        }
                        productToUpdate.Files = new List<File> { avatar };
                    }
                    db.Entry(productToUpdate).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
                catch (RetryLimitExceededException)
                {
                    ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator.");
                }
            }
            return View(productToUpdate);
        }