public ActionResult EditSupplier(EditSupplierViewModel model, HttpPostedFileBase upload) { Supplier supplier = repository.Get(model.Id); FilePath actualImage = repoFilePath.GetAll.Where(c => c.FilePathId == supplier.FilePathId).FirstOrDefault(); // get a path to image on server string actualImagePath = Request.MapPath("~/Content/Images/Suppliers/" + 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 photo = new FilePath() { FileType = FileType.supplierImage, FileName = Path.GetFileName(number + "-" + upload.FileName), }; supplier.FilePath = photo; supplier.FilePathId = photo.FilePathId; upload.SaveAs(Path.Combine(Server.MapPath("~/Content/Images/Suppliers"), photo.FileName)); } // assign properties from model supplier.DeliveryMethodId = model.DeliveryMethodId; supplier.Name = model.Name; supplier.Price = model.Price; supplier.TransportTime = model.TransportTime; // save changes repository.Update(supplier); return RedirectToAction("Index"); } // return if invalid return View(model); }
public ActionResult EditSupplier(int id) { Supplier supplier = repository.Get(id); if (supplier == null) { return HttpNotFound(); } // assign default view model's values EditSupplierViewModel model = new EditSupplierViewModel() { DeliveryMethodId = supplier.DeliveryMethodId, Id = supplier.SupplierId, Name = supplier.Name, Price = supplier.Price, TransportTime = supplier.TransportTime, Supplier = supplier, FilePathId = supplier.FilePathId, FilePath = supplier.FilePath, getDeliveryMethods = new SelectList(Retriever.GetDeliveryMethods(), "DeliveryMethodId", "Name", supplier.DeliveryMethodId) }; return View(model); }