public async Task <IActionResult> Activate(string id) { ViewBag.User = await GetCurrentUser(); if (id == null) { return(NotFound()); } var applicationUser = await _context.ApplicationUser.SingleOrDefaultAsync(m => m.Id == id); if (applicationUser == null) { return(NotFound()); } var chapterSelect = new List <SelectListItem> { new SelectListItem { Value = "0", Text = "None" } }; var chapters = _context.Chapters.OrderBy(c => c.Name).ToList(); foreach (var chapter in chapters) { chapterSelect.Add(new SelectListItem { Value = chapter.Id.ToString(), Text = chapter.Name }); } var viewModel = new InactiveActivateViewModel { Id = applicationUser.Id, FirstName = applicationUser.FirstName, LastName = applicationUser.LastName, Street1 = applicationUser.Street1, Street2 = applicationUser.Street2, City = applicationUser.City, State = applicationUser.State, ZipCode = applicationUser.ZipCode, Phone = applicationUser.PhoneNumber, Email = applicationUser.Email, WhenJoined = applicationUser.WhenJoined, WhenExpires = applicationUser.WhenExpires, NewWhenExpires = applicationUser.WhenExpires < DateTime.Now ? DateTime.Now.AddYears(1) : applicationUser.WhenExpires.AddYears(1), Chapters = chapterSelect }; return(View(viewModel)); }
public async Task <IActionResult> Activate([Bind("Id,NewWhenExpires,ChapterId")] InactiveActivateViewModel viewModel) { ViewBag.User = await GetCurrentUser(); var applicationUser = await _context.ApplicationUser.SingleOrDefaultAsync(m => m.Id == viewModel.Id); if (viewModel.Id != applicationUser.Id) { return(NotFound()); } if (ModelState.IsValid) { try { applicationUser.WhenExpires = viewModel.NewWhenExpires; applicationUser.Status = "Active"; await _userManager.AddToRoleAsync(applicationUser, "Member"); _context.Update(applicationUser); if (viewModel.ChapterId != 0) { _context.Add(new MemberChapter { ApplicationUserId = applicationUser.Id, ChapterId = viewModel.ChapterId, WhenJoined = DateTime.Now }); } await _context.SaveChangesAsync(); Response.Cookies.Append("FlashSuccess", "Member " + applicationUser.FirstName + " " + applicationUser.LastName + " was successfully renewed"); } catch (DbUpdateConcurrencyException) { if (!ApplicationUserExists(applicationUser.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction("Index")); } var chapterSelect = new List <SelectListItem> { new SelectListItem { Value = "0", Text = "None" } }; var chapters = _context.Chapters.OrderBy(c => c.Name).ToList(); foreach (var chapter in chapters) { chapterSelect.Add(new SelectListItem { Value = chapter.Id.ToString(), Text = chapter.Name }); } viewModel = new InactiveActivateViewModel { Id = applicationUser.Id, FirstName = applicationUser.FirstName, LastName = applicationUser.LastName, Street1 = applicationUser.Street1, Street2 = applicationUser.Street2, City = applicationUser.City, State = applicationUser.State, ZipCode = applicationUser.ZipCode, Phone = applicationUser.PhoneNumber, Email = applicationUser.Email, WhenJoined = applicationUser.WhenJoined, WhenExpires = applicationUser.WhenExpires, NewWhenExpires = viewModel.WhenExpires < DateTime.Now ? DateTime.Now.AddYears(1) : applicationUser.WhenExpires.AddYears(1), Chapters = chapterSelect }; return(View(viewModel)); }