public async Task <IActionResult> ChangePrimaryChapter( [Bind("MemberId,MemberChapterId")] ChangePrimaryChapterViewModel viewModel) { var currentName = "None"; var currentPrimary = await _context.MemberChapters.Include(mc => mc.Chapter).FirstOrDefaultAsync(mc => mc.ApplicationUserId == viewModel.MemberId && mc.IsPrimary); if (currentPrimary != null) { currentName = currentPrimary.Chapter.Name; currentPrimary.IsPrimary = false; _context.Update(currentPrimary); } var newPrimary = await _context.MemberChapters.Include(mc => mc.Chapter).FirstOrDefaultAsync(mc => mc.Id == viewModel.MemberChapterId); newPrimary.IsPrimary = true; _context.Update(newPrimary); var user = await _userManager.GetUserAsync(HttpContext.User); var memberNote = new ApplicationUserNote { Note = "Changed primary chapter from " + currentName + " to " + newPrimary.Chapter.Name, ApplicationUserId = viewModel.MemberId, AddedBy = user.FirstName + " " + user.LastName, WhenAdded = DateTime.Now }; _context.ApplicationUserNotes.Add(memberNote); await _context.SaveChangesAsync(); return(RedirectToAction("Details", new { id = viewModel.MemberId })); }
public async Task <IActionResult> ChangePrimaryChapter(string id) { var user = await _userManager.GetUserAsync(HttpContext.User); ViewBag.User = user.FirstName + " " + user.LastName; var member = _context.ApplicationUser.FirstOrDefault(m => m.Id == id); var memberChapters = await _context.MemberChapters .Include(mc => mc.Chapter) .Where(mc => mc.ApplicationUserId == id && mc.WhenExpires >= DateTime.Now) .ToListAsync(); var viewModel = new ChangePrimaryChapterViewModel { MemberId = id, MemberName = member.FirstName + " " + member.LastName, CurrentPrimary = "None", Chapters = new List <SelectListItem>() }; foreach (var mc in memberChapters) { if (mc.IsPrimary) { viewModel.MemberChapterId = mc.ChapterId; viewModel.CurrentPrimary = mc.Chapter.Name; } else { viewModel.Chapters.Add(new SelectListItem { Value = mc.Id.ToString(), Text = mc.Chapter.Name }); } } ViewBag.CommonName = member.CommonName; return(View(viewModel)); }