public async Task <IActionResult> RemoveQuestion(int id) { //Remove constraint from question bank 1st List <Questionsbank> lqb = _context.Questionsbank.ToList(); Questionsbank qbRemove = new Questionsbank(); foreach (Questionsbank qb in lqb) { if (qb.QuestionId == id) { qbRemove = qb; } } if (qbRemove != null) { _context.Questionsbank.Remove(qbRemove); //_context.SaveChanges(); await _context.SaveChangesAsync(); } //remove question from question table 2nd //Questions currentQuestion = _context.Questions.Find(id); Questions currentQuestion = await _context.Questions.FindAsync(id); if (currentQuestion != null) { _context.Questions.Remove(currentQuestion); //_context.SaveChanges(); await _context.SaveChangesAsync(); } return(RedirectToAction("AddQuestionToQB")); }
public IActionResult AddQuestionToQB(Questions newQuestion, int quizId) { if (ModelState.IsValid) { //Add new question to DB _context.Questions.Add(newQuestion); _context.SaveChanges(); //create relation between quizes and questions var newQB = new Questionsbank(); newQB.QuestionId = newQuestion.Id; newQB.QuizId = quizId; //Add new relationship to DB _context.Questionsbank.Add(newQB); _context.SaveChanges(); } return(RedirectToAction("AddQuestionToQB")); }