Exemplo n.º 1
0
        // GET: Questions/Create
        //public ActionResult CreateQuestion()
        //{
        //    UpdateList();
        //    ViewBag.QuestionTypeId = new SelectList(listQuestionType, "Id", "Type");
        //    ViewBag.SurveyId = new SelectList(listSurvey, "Id", "Title");
        //    return View();
        //}

        public ActionResult CreateQuestion(Guid QuestionTypeId, string Content, int?Count, string[] dynamicField)
        {
            QuestionViewModel questionViewModel = new QuestionViewModel();

            if (QuestionTypeId != null && Content != null && Content != "")
            {
                questionViewModel.Id             = Guid.NewGuid();
                questionViewModel.QuestionTypeId = QuestionTypeId;
                questionViewModel.Content        = Content;
                questionViewModel.Number         = AccountController.Count();
                questionViewModel.SurveyId       = AccountController.GetSurveyId();
                questionViewModel.Survey         = AccountController.GetSurvey();
                questionViewModel.TimeLength     = TimeSpan.FromSeconds(3);

                if (Count != null)
                {
                    if (Count < 2)
                    {
                        return(RedirectToAction("Create", "Surveys", new { error = "Scale should more than two" }));
                    }
                    questionViewModel.IsScale = true;
                    questionViewModel.Count   = (int)Count;
                    questionViewModel.AddQuestion(questionViewModel);
                }
                else if (dynamicField.Count() > 0 && dynamicField[0] != "")
                {
                    questionViewModel.AddQuestion(questionViewModel);

                    List <QuestionAnswerViewModel> answer = new List <QuestionAnswerViewModel>();
                    QuestionAnswerViewModel        q      = new QuestionAnswerViewModel();
                    int i = 1;
                    foreach (var item in dynamicField)
                    {
                        if (item != "" && item != null)
                        {
                            q             = new QuestionAnswerViewModel();
                            q.Id          = Guid.NewGuid();
                            q.QuestionId  = questionViewModel.Id;
                            q.Answer      = item;
                            q.OrderNumber = i;
                            answer.Add(q);
                            i++;
                        }
                        else
                        {
                            return(RedirectToAction("Create", "Surveys", new { error = "Answer can't be empty" }));
                        }
                    }
                    foreach (var item in answer)
                    {
                        item.AddQuestionAnswer(item);
                    }
                }
                else
                {
                    questionViewModel.IsFreeText = true;
                    questionViewModel.AddQuestion(questionViewModel);
                }
                TemporaryQuestion();
                UpdateList();
                ViewBag.QuestionTypeId = new SelectList(listQuestionType, "Id", "Type", questionViewModel.QuestionTypeId);
                ViewBag.SurveyId       = new SelectList(listSurvey, "Id", "Title", questionViewModel.SurveyId);
                return(RedirectToAction("Create", "Surveys"));
            }
            UpdateList();
            ViewBag.QuestionTypeId = new SelectList(listQuestionType, "Id", "Type", questionViewModel.QuestionTypeId);
            ViewBag.SurveyId       = new SelectList(listSurvey, "Id", "Title", questionViewModel.SurveyId);
            return(RedirectToAction("Create", "Surveys", new { error = "Question can't be empty" }));
        }