Exemplo n.º 1
0
        public ActionResult Edit(UserEditViewModel userView)
        {
            User user = null;

            try
            {
                if (ModelState.IsValid)
                {
                    user           = Repository.GetUserByID(userView.Id);
                    user.Name      = userView.Name;
                    user.Birthdate = userView.Birthdate;
                    user.Age       = userView.Age;

                    if (userView.Photo != null)
                    {
                        if (userView.ExistingPath != null)
                        {
                            string oldPath = Path
                                             .Combine(@"C:\Users\Brother\Desktop\Epam\module8\SimpleApp\SimpleApp\wwwroot\img", Path.GetFileName(userView.ExistingPath));
                            System.IO.File.Delete(oldPath);
                        }
                        string fileName = SetPhotoPath(userView);
                        string path     = "/img/" + Path.GetFileName(userView.Photo.FileName);
                        user.PhotoPath = path;
                    }

                    Repository.UpdateUser(user);
                    Repository.Save();
                    return(RedirectToAction("Index"));
                }
            }
            catch (DataException)
            {
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }
            return(View(user));
        }