Exemple #1
0
        public IActionResult Update(EditQuestionAnswerViewModel vm)
        {
            vm.Courses       = context.ReadAllCourses().ToList();
            vm.LearnOutcomes = context.ReadAllOutcomes().ToList();

            // Validate the model
            if (!ModelState.IsValid)
            {
                // Get the view model and return it
                return(View("Update", vm));
            }


            // Add the qa
            try
            {
                // TODO: add a boolean to the update questions and answers
                vm.QuesAnswer.Course       = context.ReadCourse(vm.CourseId);
                vm.QuesAnswer.LearnOutcome = context.ReadOutcome(vm.LearnOutcomeId);
                context.UpdateQuestion(vm.QuesAnswer.Id, vm.QuesAnswer);
            }
            catch
            {
                // For some reason the system could not create the Q&A. Redirect back to Update.
                ModelState.AddModelError(string.Empty, "Sorry, the system could not create the Q&A requested. Try again later.");
                return(View("Update", vm));
            }

            return(RedirectToAction("Index"));
        }
Exemple #2
0
        /// <summary>
        /// IAction method for returning a view of for updating a QuesAnswer
        /// </summary>
        /// <param name="id">Id for a QuesAnswer</param>
        /// <returns></returns>
        public IActionResult Update(int id)
        {
            var model = context.ReadQuesAnswer(id);

            var vm = new EditQuestionAnswerViewModel()
            {
                QuesAnswer = model,
                CourseId   = model.Course.Id
            };

            if (model.LearnOutcome != null)
            {
                vm.LearnOutcomeId = model.LearnOutcome.Id;
            }

            // Get the list of courses for the dropdown
            vm.Courses       = context.ReadAllCourses().ToList();
            vm.LearnOutcomes = context.ReadAllOutcomes().ToList();

            if (model == null)
            {
                return(RedirectToAction("Index"));
            }

            return(View(vm));
        }
        public ActionResult Edit(EditQuestionAnswerViewModel view)
        {
            if (ModelState.IsValid)
            {
                Answer answer = new Answer()
                {
                    ID         = view.ID,
                    QuestionID = view.QuestionID,
                    AnswererID = view.AnswererID,
                    Text       = view.Text
                };
                db.Entry(answer).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index", "Questions", new { sessionId = view.SessionID }));
            }

            return(View(view));
        }
        //GET: /Answer/Edit/5
        public ActionResult Edit(int questionId)
        {
            ApplicationUser user     = UserManager.FindById(User.Identity.GetUserId());
            Question        question = db.Questions.Find(questionId);

            //db.Entry(question).Collection(q => q.Answers).Query().Where(a => a.AnswererID.Equals(user.Id));

            if (user.UserType == ApplicationUser.UserTypes.Student)
            {
                return(RedirectToAction("Index", "Questions", new { sessionId = question.SessionID }));
            }

            //I removed this code and instead rely on the answers.Count() below to take me to Create.
            //if (!db.Answers.Any(a => a.AnswererID == user.Id && a.QuestionID == questionId))
            //{
            //	return RedirectToAction("Create", new { questionId = questionId });
            //}

            var answers = from a in db.Answers
                          where a.AnswererID == user.Id && a.QuestionID == questionId
                          select a;

            if (answers.Count() == 0)
            {
                return(RedirectToAction("Create", new { questionId = questionId }));
            }
            Answer answer = answers.First();

            EditQuestionAnswerViewModel view = new EditQuestionAnswerViewModel()
            {
                ID           = answer.ID,
                QuestionID   = answer.QuestionID,
                SessionID    = question.SessionID,
                AnswererID   = user.Id,
                QuestionText = question.Text,
                Text         = answer.Text
            };

            return(View(view));
        }