public ActionResult ViewTest(StudentTestQuestionAnswerModel testViewModel) { if (testViewModel == null) { return(View(StudentTestQuestionAnswerModel.instance.First())); } if (!ModelState.IsValid) { return(View(testViewModel)); } // List<StudentTestQuestionAnswerModel> testQuestions = System.Web.HttpContext.Current.Session["Questions"] as List<StudentTestQuestionAnswerModel>; List <StudentTestQuestionAnswerModel> testQuestions = StudentTestQuestionAnswerModel.instance; //try this StudentTestQuestionAnswerModel nextQuestion = null; foreach (StudentTestQuestionAnswerModel item in testQuestions) { if (item.QuestionID == testViewModel.QuestionID) { testQuestions.ElementAt(testQuestions.IndexOf(item)).QuestionAnswer = testViewModel.QuestionAnswer; } if (item.QuestionNum == testViewModel.QuestionNum + 1) { nextQuestion = item; } } this.ViewData = null; if (nextQuestion == null) { return(View(StudentTestQuestionAnswerModel.instance.First())); //return SaveToDatabase(testQuestions); } this.ViewData = null; return(View(nextQuestion)); }
public ActionResult ViewTest(int TestID) { ModelTest curTest = _testStore.GetTest(TestID); //init //Check test completion String Username = User.Identity.Name; List <ModelStudentAnswer> curStud = _studentAnswerStore.GetStudentAnswersByStudentByTest(Username, TestID).ToList(); List <ModelQuestion> questions = _questionStore.GetQuestionsByTest(TestID).ToList(); List <StudentTestQuestionAnswerModel> testQuestions = new List <StudentTestQuestionAnswerModel>(); int iCount = 1; questions.ForEach(cur => { StudentTestQuestionAnswerModel item = new StudentTestQuestionAnswerModel(cur.QuestionID); item.QuestionText = cur.Instruction; //init item.QuestionFlagged = false; item.QuestionNum = iCount; item.Username = User.Identity.Name; item.QuestionMark = cur.MaxMark; testQuestions.Add(item); iCount++; }); StudentTestQuestionAnswerModel.instance = testQuestions; //may the user write the test? Boolean found = false; foreach (ModelStudentAnswer cur in curStud) { //can maybe add to Token[ bool: TestSubmitted] if (cur.MarkObtained != 0 || !cur.Answer.Equals("")) { return(RedirectToAction("ViewTestResults")); } } DateTime now = DateTime.Now; DateTime startTime = curTest.StartTime; DateTime endTime = curTest.EndTime; if (startTime > now) { return(Redirect(Request.UrlReferrer.ToString())); } else if (now > endTime) { curStud.ForEach(cur => { foreach (StudentTestQuestionAnswerModel curAns in testQuestions) { if (cur.QuestionID == curAns.QuestionID) { int pos = testQuestions.IndexOf(curAns); testQuestions[pos].MarkObtained = cur.MarkObtained; testQuestions[pos].QuestionAnswer = cur.Answer; testQuestions[pos].QuestionFlagged = cur.Flagged; break; } } }); return(RedirectToAction("ViewTestResults")); } //return RedirectToAction("getDB"); return(View(testQuestions.First())); }