Exemplo n.º 1
0
        public ActionResult Edit(int id)
        {
            Livre Livre = _LivreRepository.GetLivre(id);
            LivreEditViewModel LivreEditViewModel = new LivreEditViewModel
            {
                Id    = Livre.Id,
                Name  = Livre.Name,
                price = Livre.price,

                ExistingPhotoPath = Livre.PhotoPath
            };

            return(View(LivreEditViewModel));
        }
Exemplo n.º 2
0
        private string ProcessUploadedFile(LivreEditViewModel 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);
        }
Exemplo n.º 3
0
        public ActionResult Edit(LivreEditViewModel 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 employee being edited from the database
                Livre Livre = _LivreRepository.GetLivre(model.Id);
                // Update the employee object with the data in the model object
                Livre.Name  = model.Name;
                Livre.price = model.price;

                // 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);
                    }

                    Livre.PhotoPath = ProcessUploadedFile(model);
                }


                Livre updatedLivre = _LivreRepository.Update(Livre);
                if (updatedLivre != null)
                {
                    return(RedirectToAction("index"));
                }
                else
                {
                    return(NotFound());
                }
            }

            return(View(model));
        }