// GET: Artiste/Edit/5 public ActionResult Edit(int id) { Artiste artiste = this._artisteRepository.GetById(id); ArticteEditModel artisteModel = new ArticteEditModel(); artisteModel.Id = artiste.Id; artisteModel.Name = artiste.Name; artisteModel.ExistingPhotoPath = artiste.Image; return(View(artisteModel)); }
private string ProcessUploadedFile(ArticteEditModel model) { string uniqueFileName = null; if (model.Photo != null) { string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "images"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.Photo.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.Photo.CopyTo(fileStream); } } return(uniqueFileName); }
public ActionResult Edit(ArticteEditModel model) { if (ModelState.IsValid) { // Retrieve the employee being edited from the database Artiste artiste = _artisteRepository.GetById(model.Id); // Update the employee object with the data in the model object artiste.Name = model.Name; // If the user wants to change the photo, a new photo will be // uploaded and the Photo property on the model object receives // the uploaded photo. If the Photo property is null, user did // not upload a new photo and keeps his existing photo if (model.Photo != null) { // If a new photo is uploaded, the existing photo must be // deleted. So check if there is an existing photo and delete if (model.ExistingPhotoPath != null) { string filePath = Path.Combine(hostingEnvironment.WebRootPath, "images", model.ExistingPhotoPath); System.IO.File.Delete(filePath); } // Save the new photo in wwwroot/images folder and update // PhotoPath property of the employee object which will be // eventually saved in the database artiste.Image = ProcessUploadedFile(model); } // Call update method on the repository service passing it the // employee object to update the data in the database table Artiste updatedArtiste = _artisteRepository.Edit(artiste); if (updatedArtiste != null) { return(RedirectToAction("index")); } else { return(NotFound()); } } return(View(model)); }