public async Task <IActionResult> Edit(QuestionManageEditPostViewModel model) { var question = await _arDbContext.Questions.SingleOrDefaultAsync(q => q.ID == model.ID); if (question == null) { return(NotFound()); } if (ModelState.IsValid) { question.GrammarIDString = QuestionManageDataBuilder.ConvertGrammarIDListToGrammarIDString(model.SelectedGrammarIDCollection); Mapper.Map <QuestionManageEditPostViewModel, Question>(model, question); try { _arDbContext.Update(question); await _arDbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_arDbContext.Questions.Any(q => q.ID == question.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } var vm = Mapper.Map <QuestionManageEditPostViewModel, QuestionManageEditGetViewModel>(model); vm.EntireGrammarCollection = await _arDbContext.Grammars.ToListAsync(); vm.ApplicationUserPresenterCollection = await ApplicationUserHandler.ConvertApplicationUsersToPresenterCollection(_userManager, _roleManager, _userManager.Users.ToList()); return(View(vm)); }
public async Task <IActionResult> Create(QuestionManageCreatePostViewModel model) { if (ModelState.IsValid) { var question = Mapper.Map <QuestionManageCreatePostViewModel, Question>(model); // I think grammar should be a multiple select and handled here. question.GrammarIDString = QuestionManageDataBuilder.ConvertGrammarIDListToGrammarIDString(model.SelectedGrammarIDCollection); question.CreateDate = DateTime.Now; question.EditorID = (await _userManager.GetUserAsync(HttpContext.User)).Id; _arDbContext.Questions.Add(question); await _arDbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } //if isValid is false var vm = Mapper.Map <QuestionManageCreatePostViewModel, QuestionManageCreateGetViewModel>(model); vm.EntireGrammarCollection = _arDbContext.Grammars.ToList(); return(View(vm)); }