public ActionResult EditAnswerPost(int questionId, int? id) { Answer item = null; Question question = null; try { question = db.TestQuestion.Find(questionId); if (question == null) throw new ArgumentException("Вопрос не найден."); if (id.HasValue) { item = db.TestAnswer.SingleOrDefault(i => i.Id == id); if (item == null) throw new ArgumentException("Ответ не найден."); } else { item = new Answer() { Question = question }; } TryUpdateModel(item, "Answer", new[] { "Text", "Right" }); if (ModelState.IsValid) { if (id == null) db.TestAnswer.Add(item); else db.Entry<Answer>(item).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Answers", new { id = question.Id }); } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } return View(new TestViewModel(db, question.Test, question, item)); }
public TestViewModel(DataContext db, TestMain item, Question question, Answer answer) : this(db, item, question) { Answer = answer; }
// // GET: /Admin/Test/EditAnswer public ActionResult EditAnswer(int questionId, int? id) { Answer item = null; Question question = null; try { question = db.TestQuestion.Find(questionId); if (question == null) throw new ArgumentException("Вопрос не найден."); if (id.HasValue) { item = db.TestAnswer.SingleOrDefault(i => i.Id == id); if (item == null) throw new ArgumentException("Ответ не найден."); } else { item = new Answer() { Question = question }; } } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } return View(new TestViewModel(db, question.Test, question, item)); }