public IActionResult AddQuestions() { var lessonsDb = this.subjectService.GetLessons() .OrderBy(lesson => lesson.Title) .ToList(); var lessonsDto = this.mapper.Map <List <LessonsViewModel> >(lessonsDb); var model = new QuestionsAddViewModel { Lessons = lessonsDto, }; return(this.View(model)); }
public async Task <IActionResult> AddQuestionsAsync(QuestionsAddViewModel model) { try { if (model.Questions.Count < 1) { this.ModelState.AddModelError(string.Empty, "Моля добавете поне един въпрос."); return(this.View(model)); } var test = this.testService .GetTests() .Include("Lesson") .FirstOrDefault(t => t.LessonId == model.LessonId); foreach (var question in model.Questions) { test.Questions.Add(new Question { QuestionId = Guid.NewGuid().ToString(), Title = question, }); } } catch (Exception exception) { this.TempData["ErrorMsg"] = exception.Message; return(this.Redirect("/Home/ErrorView")); } await this.testService.SaveChangesAsync(); this.TempData["SuccessMsg"] = "Въпросите бяха записани"; return(this.Redirect("/Home/Success")); }