public ActionResult Create(HttpPostedFileBase newImage, Producer producer, string submitButtons) { if (newImage != null) { String filePath = Guid.NewGuid() + Path.GetExtension(newImage.FileName); if (!string.IsNullOrWhiteSpace(producer.LogoPath)) System.IO.File.Delete(producer.FullLogoPath); producer.LogoPath = filePath; ModelState["LogoPath"].Errors.Clear(); newImage.SaveAs(producer.FullLogoPath); } if (submitButtons == "Create") { if (ModelState.IsValid) { db.Producers.Add(producer); db.SaveChanges(); return RedirectAfterSuccess(); } } return View(producer); }
public ActionResult Edit(HttpPostedFileBase newImage, Producer producer, string submitButtons) { if (newImage != null) { String filePath = Guid.NewGuid() + Path.GetExtension(newImage.FileName); if (!string.IsNullOrWhiteSpace(producer.LogoPath)) { string prevValue = db.Producers.Where(item => item.ProducerId == producer.ProducerId).Select(item => item.LogoPath) .SingleOrDefault(); if (prevValue != producer.LogoPath) System.IO.File.Delete(producer.FullLogoPath); } producer.LogoPath = filePath; newImage.SaveAs(producer.FullLogoPath); } if (submitButtons == "Save") { if (ModelState.IsValid) { string prevValue = db.Producers.Where(item => item.ProducerId == producer.ProducerId).Select(item => item.LogoPath) .SingleOrDefault(); //remove old image if (prevValue != producer.LogoPath) System.IO.File.Delete(String.Format("{0}Content{1}Images{1}Producers{1}{2}", AppDomain.CurrentDomain.BaseDirectory, Path.DirectorySeparatorChar, prevValue)); db.Entry(producer).State = EntityState.Modified; db.SaveChanges(); return RedirectAfterSuccess(); } } return View(producer); }