Exemple #1
0
        public ActionResult Edit(EditViewModel model)
        {
            // Check if the provided data is valid, if not rerender the edit view
            // so the user can correct and resubmit the edit form
            if (ModelState.IsValid)
            {
                // Retrieve the certification being edited from the database
                Certification certification = certificationRepository.GetById(model.CertificationId);
                // Update the certification object with the data in the model object
                certification.CertificationName = model.CertificationName;
                certification.Description       = model.Description;
                certification.Price             = model.Price;
                certification.CategorieId       = model.CategorieId;
                certification.Categorie         = model.Categorie;

                // 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 certification object which will be
                    // eventually saved in the database
                    certification.PhotoPath = ProcessUploadedFile(model);
                }

                // Call update method on the repository service passing it the
                // certification object to update the data in the database table
                Certification updatedCertification = certificationRepository.Edit(certification);
                if (updatedCertification != null)
                {
                    return(RedirectToAction("index"));
                }
                else
                {
                    return(NotFound());
                }
            }

            return(View(model));
        }