public TestViewModel(DataContext db, TestMain item, Question question, Answer answer)
     : this(db, item, question)
 {
     Answer = answer;
 }
        public ActionResult EditQuestionPost(int testId, int? id)
        {
            Question item = null;
            TestMain test = null;

            try
            {
                test = db.TestMain.Find(testId);
                if (test == null)
                    throw new ArgumentException("Тест не найден.");

                if (id.HasValue)
                {
                    item = db.TestQuestion.SingleOrDefault(i => i.Id == id);
                    if (item == null)
                        throw new ArgumentException("Вопрос не найден.");
                }
                else
                {
                    item = new Question() { Test = test };
                }

                TryUpdateModel(item, "Question", new[] { "Text" });

                if (ModelState.IsValid)
                {
                    if (id == null)
                        db.TestQuestion.Add(item);
                    else
                        db.Entry<Question>(item).State = EntityState.Modified;

                    db.SaveChanges();
                    return RedirectToAction("Questions", new { id = test.Id });
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }

            return View(new TestViewModel(db, test, item));
        }
 public TestViewModel(DataContext db, TestMain item, Question question)
     : this(db, item)
 {
     Question = question;
 }
        //
        // GET: /Admin/Test/EditQuestion
        public ActionResult EditQuestion(int testId, int? id)
        {
            Question item = null;
            TestMain test = null;

            try
            {
                test = db.TestMain.Find(testId);
                if (test == null)
                    throw new ArgumentException("Тест не найден.");

                if (id.HasValue)
                {
                    item = db.TestQuestion.SingleOrDefault(i => i.Id == id);
                    if (item == null)
                        throw new ArgumentException("Вопрос не найден.");
                }
                else
                {
                    item = new Question() { Test = test };
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError(string.Empty, ex.Message);
            }

            return View(new TestViewModel(db, test, item));
        }