// // GET: /Answer/Create public ActionResult Create(int id) { Question question = context.Questions.SingleOrDefault(b => b.QuestionId == id); ViewBag.QuestLabel = question.QuestionLabel; Answer answer = new Answer(); answer.QuestionId = id; return View(answer); }
public ActionResult Create(Answer answer) { try { if (ModelState.IsValid) { Question question = context.Questions.SingleOrDefault(b => b.QuestionId == answer.QuestionId); question.Answers.Add(answer); context.SaveChanges(); return RedirectToAction("Details", "Question", new { id = question.QuestionId }); } return View(); } catch(Exception ex) { return RedirectToAction("Error", "Shared", ex.Message); } }
public ActionResult AddAnswer(FormCollection collection) { try { //Création d'une nouvelle réponse Answer answer = new Answer(); //On récupère le questionId de la réponse answer.QuestionId = Convert.ToInt32(Request.Form["listQuestions"].ToString()); //On retourne la réponse à la vue return View(answer); } catch (Exception ex) { return RedirectToAction("Error", "Shared", ex.Message); } }
public ActionResult Edit(int id, Answer answer) { try { if (ModelState.IsValid) { using (context) { Answer _answer = context.Answers.Single(p => p.AnswerId == id); _answer.AnswerLabel = answer.AnswerLabel; context.Entry(_answer).State = EntityState.Modified; context.SaveChanges(); return RedirectToAction("Index"); } } return View(answer); } catch (Exception ex) { return RedirectToAction("Error", "Shared", ex.Message); } }
public ActionResult Delete(int id, Answer answer) { try { //Get answer to delete Answer _answer = context.Answers.Single(p => p.AnswerId == id); #region DeleteStats //Select All stats to this answer List<Stat> statsToDelete = context.Stats.Where(i => i.AnswerId == id).ToList(); //On supprime chaque stat foreach (Stat statToDelete in statsToDelete) { context.Stats.Remove(statToDelete); } #endregion //Delete answer context.Answers.Remove(_answer); context.SaveChanges(); return RedirectToAction("Index"); } catch (Exception ex) { return RedirectToAction("Error", "Shared", ex.Message); } }