Exemplo n.º 1
0
 public async Task<ActionResult> UserProfile(int userId, string activeSection)
 {
     User user = await UserManager.FindByIdAsync(userId);
     if (userId > 0 && user != null)
     {
         var viewModel = new UserProfileViewModel
         {
             Id = user.Id,
             UserName = user.UserName,
             FirstName = user.FirstName,
             LastName = user.LastName,
             AboutMe = user.AboutMe,
             Email = user.Email,
             PasswordHash = user.PasswordHash,
             Address1 = user.Address1,
             Address2 = user.Address2,
             City = user.City,
             State = user.State,
             Country = user.Country,
             ZipCode = user.ZipCode,
             ImageData = user.ImageData,
             ImageMimeType = user.ImageMimeType
         };
         using (var db = new EfdbContext())
         {
             viewModel.Recipes = new List<Recipe>();
             viewModel.Recipes = db.Recipes.Where(r => r.UserId == userId).ToList();
         }
         ViewBag.ActiveSection = activeSection;
         return View(viewModel);
     }
     
     return View("Error", new[] {"No user was found!"});
 }
Exemplo n.º 2
0
        public async Task<ActionResult> EditProfile(UserProfileViewModel userViewModel, HttpPostedFileBase image = null)
        {
            if (ModelState.IsValid)
            {
                User user = await UserManager.FindByIdAsync(userViewModel.Id);

                if (user != null)
                {
                    user.Id = userViewModel.Id;
                    user.UserName = userViewModel.UserName;
                    user.Email = userViewModel.Email;
                    user.PasswordHash = userViewModel.PasswordHash;
                    user.FirstName = userViewModel.FirstName;
                    user.LastName = userViewModel.LastName;
                    user.AboutMe = userViewModel.AboutMe;
                    user.Address1 = userViewModel.Address1;
                    user.Address2 = userViewModel.Address2;
                    user.City = userViewModel.City;
                    user.State = userViewModel.State;
                    user.Country = userViewModel.Country;
                    user.ZipCode = userViewModel.ZipCode;
                    user.ImageData = userViewModel.ImageData;
                    user.ImageMimeType = userViewModel.ImageMimeType;

                    if (image != null)
                    {
                        user.ImageMimeType = image.ContentType;
                        user.ImageData = new byte[image.ContentLength];
                        image.InputStream.Read(user.ImageData, 0, image.ContentLength);
                    }

                    using (var db = new EfdbContext())
                    {
                        userViewModel.Recipes = new List<Recipe>();
                        userViewModel.Recipes = db.Recipes.Where(r => r.UserId == userViewModel.Id).ToList();
                    }

                    IdentityResult validationResult = await UserManager.UserValidator.ValidateAsync(user);
                    if (validationResult.Succeeded)
                    {
                        IdentityResult updateResult = await UserManager.UpdateAsync(user);
                        if (updateResult.Succeeded)
                        {
                            ViewBag.ActiveSection = "Profile";
                            return View("UserProfile", userViewModel);
                        }
                        foreach (var error in updateResult.Errors)
                        {
                            ModelState.AddModelError("", error);
                        }
                    }
                    foreach (var error in validationResult.Errors)
                    {
                        ModelState.AddModelError("", error);
                    }
                }
                ModelState.AddModelError("", "There was an error accessing user data. Please try again later!");
            }
            ViewBag.ActiveSection = "EditProfile";
            return View("UserProfile", userViewModel);
        }