public virtual ActionResult Index() { var questions = _service.GetQuestions(); var answers = _service.GetAvalivableAnswers(); var model = new QuestionaireViewModel(); model.SetQuestions(questions, answers); return(View(model)); }
public virtual ActionResult Index(QuestionaireViewModel model) { var user = new User() { MajorId = model.User.MajorId, StudyYearId = model.User.StudyYearId, UserSexId = model.User.UserSexId }; var answersList = new List <Answer>(); foreach (var question in model.Questions) { answersList.Add(new Answer() { QuestionId = question.QuestionId, AvalivableAnswerId = question.SelectedAnswerId, AnswerText = question.AnswerText }); } if (_service.TrySaveData(user, answersList)) { return(RedirectToAction(MVC.Home.ThankYou())); } else { ModelState.AddModelError("", "Należy odpowiedzieć na wszystkie pytnia"); var answers = _service.GetAvalivableAnswers(); foreach (var question in model.Questions) { foreach (var answer in answers.Where(x => x.QuestionId == question.QuestionId)) { var item = new SelectListItem(); item.Text = answer.Text; item.Value = answer.AvalivableAnswerId.ToString(); if (question.SelectedAnswerId > 0) { item.Selected = true; } question.AvalivableAnswers.Add(item); } } } #region ToDelete //bool everythingOk = false; //var basicQuestionAnswered = model.Questions.Where(x => x.QuestionId <= 7).All(x => x.SelectedAnswerId > 0); //if (basicQuestionAnswered) //{ // var selectedAnswerId_6 = model.Questions[6].SelectedAnswerId; // var selectedAnswer_6 = _service.GetAvalivableAnswerById(selectedAnswerId_6); // if (selectedAnswer_6 != null && selectedAnswer_6.Text == "Nie") // { // model.Questions[7].SelectedAnswerId = 0; // model.Questions[7].AnswerText = ""; // model.Questions[8].SelectedAnswerId = 0; // model.Questions[8].AnswerText = ""; // model.Questions[9].SelectedAnswerId = 0; // model.Questions[9].AnswerText = ""; // model.Questions[10].SelectedAnswerId = 0; // model.Questions[10].AnswerText = ""; // model.Questions[11].SelectedAnswerId = 0; // model.Questions[11].AnswerText = ""; // everythingOk = true; // } // else // { // if (model.Questions[7].SelectedAnswerId > 0 && model.Questions[8].SelectedAnswerId > 0 && model.Questions[9].SelectedAnswerId > 0 && model.Questions[11].SelectedAnswerId > 0) // { // var selectedAnswerId_9 = model.Questions[9].SelectedAnswerId; // var selectedAnswer_9 = _service.GetAvalivableAnswerById(selectedAnswerId_9); // if (selectedAnswer_9 != null && selectedAnswer_9.Text == "Tak, (komu?)") // { // model.Questions[10].SelectedAnswerId = 0; // model.Questions[10].AnswerText = ""; // everythingOk = true; // } // else // { // if (model.Questions[10].SelectedAnswerId == 0) // { // everythingOk = false; // } // else // { // everythingOk = true; // } // } // } // else // { // everythingOk = false; // } // } //} //else //{ // everythingOk = false; //} //if (everythingOk) //{ // var major = _service.GetMajorById(model.User.MajorId); // var studyYear = _service.GetStudyYearById(model.User.StudyYearId); // var userSex = _service.GetUserSexById(model.User.UserSexId); // var user = new User() // { // MajorId = major.MajorId, // Major = major.Description, // StudyYearId = studyYear.StudyYearId, // StudyYear = studyYear.Description, // UserSexId = userSex.UserSexId, // UserSex = userSex.Description // }; // var userId = _service.CreateUser(user).UserId; // var answersList = new List<Answer>(); // foreach(var question in model.Questions) // { // answersList.Add(new Answer() // { // QuestionId = question.QuestionId, // AvalivableAnswerId = question.SelectedAnswerId, // AnswerText = question.AnswerText, // UserId = userId // }); // } // _service.AddAnswers(answersList); // return RedirectToAction(MVC.Home.ThankYou()); //} //else //{ // ModelState.AddModelError("", "Należy odpowiedzieć na wszystkie pytnia"); // var answers = _service.GetAvalivableAnswers(); // foreach (var question in model.Questions) // { // foreach (var answer in answers.Where(x => x.QuestionId == question.QuestionId)) // { // var item = new SelectListItem(); // item.Text = answer.Text; // item.Value = answer.AvalivableAnswerId.ToString(); // if (question.SelectedAnswerId > 0) // { // item.Selected = true; // } // question.AvalivableAnswers.Add(item); // } // } //} #endregion return(View(model)); }