public TakeSurveyViewModel Map(TakeSurveyViewModel responses)
        {
            Survey survey = _surveyRepository.GetSurvey(responses.SurveyId);

            TakeSurveyViewModel viewModel = new TakeSurveyViewModel();

            //  Map the survey to the survey view model.
            viewModel.SurveyId = survey.SurveyId;
            viewModel.Title = survey.Title;
            viewModel.UserName = survey.User.UserName;
            viewModel.StatusDate = string.Format("{0:d}", survey.StatusDate);
            viewModel.CategoryDescription = survey.Category.Description;

            //  Map the questions to the view model
            viewModel.Questions = new List<SurveyQuestionsViewModel>();

            //foreach (Core.Model.Question q in survey.Questions)
            foreach (SurveyQuestionsViewModel q in responses.Questions)
            {
                //  Get the question and available responses from the repository.
                Core.Model.Question questionInfo = _questionRepository.GetQuestion(Convert.ToInt64(q.QId_SeqNo.Split('_')[0]));

                SurveyQuestionsViewModel questionViewModel = new SurveyQuestionsViewModel();
                questionViewModel.QId_SeqNo = q.QId_SeqNo;

                questionViewModel.SequenceNumber = questionInfo.SequenceNumber;
                questionViewModel.Text = questionInfo.Text;
                questionViewModel.Answer = q.Answer;

                //  Map the responses to the question
                questionViewModel.Responses = new List<SurveyResponsesViewModel>();
                //  this causes a DBContext Disposed error if it's visited a 2nd time.
                foreach (Core.Model.AvailableResponse r in questionInfo.AvailableResponses)
                {
                    SurveyResponsesViewModel responsesViewModel = new SurveyResponsesViewModel();
                    responsesViewModel.QId_SeqNo = q.QId_SeqNo;
                    responsesViewModel.Answer = q.Answer.ToString();
                    responsesViewModel.LikertScaleNumber = r.LikertScaleNumber;
                    responsesViewModel.Text = r.Text;
                    questionViewModel.Responses.Add(responsesViewModel);
                }
                viewModel.Questions.Add(questionViewModel);
            }
            return viewModel;
        }
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            //  Create a new model if it's not already defined.
            TakeSurveyViewModel model = (TakeSurveyViewModel)bindingContext.Model ?? new TakeSurveyViewModel();

            //  retrieve the information from the view.
            model.SurveyId = Convert.ToInt64(controllerContext.HttpContext.Request["SurveyId"]);

            //  Set new questions model
            model.Questions = new List<SurveyQuestionsViewModel>();

            //  Process each quetion in the survey
            foreach (string qs in (controllerContext.HttpContext.Request["item.QId_SeqNo"]).Split(','))
            {
                //  Split questionId and SequenceNumber from input field.
                string[] str = qs.Split('_');
                string q = str[0];
                string s = str[1];
                //  Instantiate the question in the viewModel
                SurveyQuestionsViewModel question = new SurveyQuestionsViewModel();
                question.QId_SeqNo = qs;

                //  Find the selected response
                var response = controllerContext.HttpContext.Request["LikertScaleNumber_" + qs];
                if (response == null)
                {
                    //  It's not there, so add error to modelstate: the user hasn't answered the question.
                    bindingContext.ModelState.AddModelError("", string.Format("You have not answered question {0}", s));
                    bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue("LikertScaleNumber_" + qs));
                    question.Answer = "";
                }
                else
                {
                    //  Add the answer to the question viewModel.
                    question.Answer = response;
                }
                //  Add the question to the survey
                model.Questions.Add(question);
            }
            return model;
        }
        /// <summary>
        /// Map the Survey to the TakeSurveyViewModel.
        /// </summary>
        /// <remarks>
        /// This mapping is done explicitly as the target structure is quite complex
        /// and the amount of exceptions required if AutoMapper were used would not
        /// have a significant effect on the amount of code.
        /// Additionally, the top level represents a single survey, but the
        /// lower levels represent collections of objects.
        /// </remarks>
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            Survey survey = (Survey) filterContext.Controller.ViewData.Model;

            TakeSurveyViewModel viewModel = new TakeSurveyViewModel();

            //  Map the survey to the survey view model.
            viewModel.SurveyId = survey.SurveyId;
            viewModel.Title = survey.Title;
            viewModel.UserName = survey.User.UserName;
            viewModel.StatusDate = string.Format("{0:d}", survey.StatusDate);
            viewModel.CategoryDescription = survey.Category.Description;

            //  Map the questions to the view model
            viewModel.Questions = new List<SurveyQuestionsViewModel>();
            foreach (Core.Model.Question q in survey.Questions)
            {
                SurveyQuestionsViewModel questionViewModel = new SurveyQuestionsViewModel();
                questionViewModel.QId_SeqNo = q.QuestionId.ToString() + "_" + q.SequenceNumber.ToString();
                questionViewModel.SequenceNumber = q.SequenceNumber;
                questionViewModel.Text = q.Text;

                //  Map the responses to the question
                questionViewModel.Responses = new List<SurveyResponsesViewModel>();
                foreach (Core.Model.AvailableResponse r in q.AvailableResponses)
                {
                    SurveyResponsesViewModel responsesViewModel = new SurveyResponsesViewModel();
                    responsesViewModel.QId_SeqNo = questionViewModel.QId_SeqNo;
                    responsesViewModel.LikertScaleNumber = r.LikertScaleNumber;
                    responsesViewModel.Text = r.Text;
                    questionViewModel.Responses.Add(responsesViewModel);
                }
                viewModel.Questions.Add(questionViewModel);
            }

            filterContext.Controller.ViewData.Model = viewModel;
        }
Example #4
0
 /// <summary>
 /// Returns a populated TakeSurveyViewModel class
 /// </summary>
 /// <returns></returns>
 public TakeSurveyViewModel SetTakeSurveyViewModel_3()
 {
     TakeSurveyViewModel vM = new TakeSurveyViewModel();
     vM.CategoryDescription = null;
     vM.StatusDate = null;
     vM.SurveyId = 3;
     vM.Title = null;
     vM.UserName = null;
     vM.Questions = new List<SurveyQuestionsViewModel>();
     //  Add response to question1
     SurveyQuestionsViewModel response1 = new SurveyQuestionsViewModel();
     response1.Answer = "5";
     response1.QId_SeqNo="1_1";
     response1.SequenceNumber = 0;
     response1.Responses = null;
     response1.Text=null;
     vM.Questions.Add(response1);
     //  Add response to question 2
     SurveyQuestionsViewModel response2 = new SurveyQuestionsViewModel();
     response2.Answer = "5";
     response2.QId_SeqNo="1_2";
     response2.SequenceNumber = 0;
     response2.Responses = null;
     response2.Text=null;
     vM.Questions.Add(response2);
     //  Return it
     return vM;
 }