public ActionResult EditColor(EditColorViewModel model, HttpPostedFileBase upload) { Color color = repository.Get(model.Id); FilePath actualImage = repoFilePath.GetAll.Where(c => c.FilePathId == color.FilePathId).FirstOrDefault(); // assign name from model color.ColorName = model.Name; // get a path to image on server string actualImagePath = Request.MapPath("~/Content/Images/Colors/" + actualImage.FileName); if (ModelState.IsValid) { // check if upload exists // if exists delete old from server, // assign new to color and save on server if (upload != null && upload.ContentLength > 0) { System.IO.File.Delete(actualImagePath); Guid number = Guid.NewGuid(); FilePath colorImage = new FilePath { FileType = FileType.colorImage, FileName = Path.GetFileName(number + "-" + upload.FileName) }; color.FilePath = colorImage; color.FilePathId = colorImage.FilePathId; upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/Colors"), colorImage.FileName)); } repository.Update(color); return RedirectToAction("Index"); } // return if invalid return View(model); }
public ActionResult EditColor(int id) { Color color = repository.Get(id); if (color == null) { return HttpNotFound(); } // assign default view model's values EditColorViewModel model = new EditColorViewModel() { Id = color.ColorId, Name = color.ColorName, FilePath = color.FilePath }; return View(model); }