public ActionResult Create(int id, SurveyResponse surveyResponse, QuestionAnswerParameter[] questions) { var survey = Repository.OfType<Survey>().GetNullableById(id); if (survey == null || !survey.IsActive) { Message = "Survey not found or not active."; return this.RedirectToAction<ErrorController>(a => a.Index()); } var surveyResponseToCreate = new SurveyResponse(survey); if (questions == null) { questions = new QuestionAnswerParameter[0]; } ModelState.Clear(); //Set non-dynamic surveyResponse values surveyResponseToCreate.StudentId = surveyResponse.StudentId; surveyResponseToCreate.UserId = CurrentUser.Identity.Name.ToLower(); //Check each question, create an answer for it if there isn't one. var length = questions.Length; for (int i = 0; i < length; i++) { var question = Repository.OfType<Question>().GetNullableById(questions[i].QuestionId); Check.Require(question != null, string.Format("Question not found.\n SurveyId: {0}\n QuestionId: {1}\n Question #: {2}", id, questions[i].QuestionId, i)); Check.Require(question.Category.IsActive, string.Format("Related Category is not active for question Id {0}", questions[i].QuestionId)); Check.Require(question.Category.IsCurrentVersion, string.Format("Related Category is not current version for question Id {0}", questions[i].QuestionId)); Check.Require(question.Survey.Id == survey.Id, string.Format("Related Survey does not match passed survey {0}--{1}", question.Survey.Id, survey.Id)); Answer answer; if (surveyResponseToCreate.Answers.Where(a => a.Question.Id == question.Id).Any()) { answer = surveyResponseToCreate.Answers.Where(a => a.Question.Id == question.Id).First(); } else { answer = new Answer(); } //Score question and specify any errors questions[i] = _scoreService.ScoreQuestion(surveyResponseToCreate.Survey.Questions.AsQueryable(), questions[i]); if (questions[i].Invalid && !questions[i].BypassQuestion) { ModelState.AddModelError(string.Format("Questions[{0}]", i), questions[i].Message); } if (questions[i].BypassQuestion) { answer.OpenEndedAnswer = null; answer.Response = null; answer.Score = 0; answer.BypassScore = true; questions[i].Answer = string.Empty; questions[i].ResponseId = 0; } else { answer.OpenEndedAnswer = questions[i].OpenEndedNumericAnswer; if (question.IsOpenEnded && question.OpenEndedQuestionType == (int)QuestionType.TimeRange) { answer.OpenEndedStringAnswer = string.Format("{0}_{1}", questions[i].Answer, questions[i].AnswerRange); } else { answer.OpenEndedStringAnswer = questions[i].Answer; // The actual answer they gave. } answer.Response = Repository.OfType<Response>().GetNullableById(questions[i].ResponseId); answer.Score = questions[i].Score; answer.BypassScore = false; } answer.Question = question; answer.Category = question.Category; surveyResponseToCreate.AddAnswers(answer); } surveyResponseToCreate.TransferValidationMessagesTo(ModelState); if (survey.Questions.Where(a => a.IsActive && a.Category.IsActive && a.Category.IsCurrentVersion).Count() != questions.Count()) { Message = "You must answer all survey questions."; ModelState.AddModelError("Question", "You must answer all survey questions."); } if (ModelState.IsValid) { _scoreService.CalculateScores(Repository, surveyResponseToCreate); _surveyResponseRepository.EnsurePersistent(surveyResponseToCreate); Message = "Below are the customized goals for the survey you entered. You can use the \"Print Results\" link below to print this individual goal sheet. If you would like to print a text-only version or goal sheets for multiple participants at one time go to the Educators Dashboard and select the Review & Print section for the survey you are working with."; return this.RedirectToAction(a => a.Results(surveyResponseToCreate.Id, null)); } else { //foreach (var modelState in ModelState.Values.Where(a => a.Errors.Count() > 0)) //{ // var x = modelState; //} if (string.IsNullOrWhiteSpace(Message)) { Message = "Please correct all errors and submit."; } var viewModel = SurveyResponseViewModel.Create(Repository, survey); viewModel.SurveyResponse = surveyResponse; viewModel.SurveyAnswers = questions; return View(viewModel); } }