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)); }
public async Task <IActionResult> Create(ExerciseManageCreatePostViewModel model) { if (ModelState.IsValid) { var exercise = Mapper.Map <ExerciseManageCreatePostViewModel, Exercise>(model); exercise.CreateDate = DateTime.Now; exercise.EditorID = (await _userManager.GetUserAsync(HttpContext.User)).Id; _arDbContext.Exercises.Add(exercise); await _arDbContext.SaveChangesAsync(); if (model.SelectedQuestionIDCollection != null) { foreach (var questionID in model.SelectedQuestionIDCollection) { var question = _arDbContext.Questions.SingleOrDefault(q => q.ID == questionID); if (question == null) { return(NotFound()); } var exerciseQuestionRelation = new ExerciseQuestionRelation { ExerciseID = exercise.ID, QuestionID = question.ID }; _arDbContext.ExerciseQuestionRelationMap.Add(exerciseQuestionRelation); await _arDbContext.SaveChangesAsync(); } } return(RedirectToAction(nameof(Index))); } //if isValid is false var vm = Mapper.Map <ExerciseManageCreatePostViewModel, ExerciseManageCreateGetViewModel>(model); vm.EntireQuestionCollection = _arDbContext.Questions.ToList(); return(View(vm)); }