public IActionResult EditProfile(int profileId) { AdvertiserProfile advertiserProfile = _context.AdvertiserProfiles .Include(ap => ap.AdvertiserProfileTags) .Include(ap => ap.User) .Where(ap => ap.UserId == SessionExtensions.GetInt32(HttpContext.Session, "UserId")) .FirstOrDefault(); AdvertiserEditProfileViewModel advertiserEditProfileViewModel = new AdvertiserEditProfileViewModel { ProfileId = profileId, GivenName = advertiserProfile.GivenName, FamilyName = advertiserProfile.FamilyName, CompanyName = advertiserProfile.CompanyName, ProfilePicture = advertiserProfile.ProfilePicture, Password = _context.Users .Where(u => u.UserId == SessionExtensions.GetInt32(HttpContext.Session, "UserId")) .FirstOrDefault().Password, Tags = _context.Tags .Select(t => new SelectListItem { Text = t.TypeName, Value = t.TagId.ToString(), Selected = advertiserProfile.AdvertiserProfileTags .Where(apt => apt.TagId == t.TagId) .FirstOrDefault() != null }) .ToList(), SelectedTags = advertiserProfile.AdvertiserProfileTags.Select(apt => apt.TagId.ToString()).ToArray() }; return(View(advertiserEditProfileViewModel)); }
public IActionResult EditProfile([Bind("ProfileId,NewProfilePicture,GivenName,FamilyName,CompanyName,Password,ConfirmPassword,SelectedTags")] AdvertiserEditProfileViewModel advertiserEditProfileViewModel) { advertiserEditProfileViewModel.ProfilePicture = _context.AdvertiserProfiles .Where(ap => ap.ProfileId == advertiserEditProfileViewModel.ProfileId) .FirstOrDefault() .ProfilePicture; if (ModelState.IsValid) { AdvertiserProfile advertiserProfile = _context.AdvertiserProfiles.Include(ap => ap.User).Where(ap => ap.ProfileId == advertiserEditProfileViewModel.ProfileId).FirstOrDefault(); advertiserProfile.GivenName = advertiserEditProfileViewModel.GivenName; advertiserProfile.FamilyName = advertiserEditProfileViewModel.FamilyName; advertiserProfile.CompanyName = advertiserEditProfileViewModel.CompanyName; advertiserProfile.User.Password = advertiserEditProfileViewModel.Password; if (advertiserEditProfileViewModel.NewProfilePicture != null) { var profilePictureUri = new ProfilePictureController() .Upload( advertiserEditProfileViewModel.NewProfilePicture, DateTime.Now.ToFileTime() + "-" + advertiserEditProfileViewModel.GivenName + "-" + advertiserEditProfileViewModel.FamilyName + "-ProfilePicture"); advertiserProfile.ProfilePicture = profilePictureUri.Result; } _context.Update(advertiserProfile); _context.SaveChanges(); string fileName = advertiserEditProfileViewModel.ProfilePicture.Split('/').Last(); new ProfilePictureController().DeleteProfilePicture(fileName); return(RedirectToAction(nameof(Index))); } advertiserEditProfileViewModel.Tags = _context.Tags .Select(t => new SelectListItem { Text = t.TypeName, Value = t.TagId.ToString(), Selected = advertiserEditProfileViewModel.SelectedTags .Where(st => int.Parse(st) == t.TagId) .FirstOrDefault() != null }) .ToList(); return(View(advertiserEditProfileViewModel)); }