示例#1
0
        // GET: Survey
        public ActionResult Index()
        {
            // Create the list of possible answers for questions
            // Each questions may have a different number of answers
            // In this case all questions have 3 possible answers
            var possibleAnswers = new List <AnswerViewModel3>
            {
                new AnswerViewModel3 {
                    Id = 1, Text = "Fair"
                },
                new AnswerViewModel3 {
                    Id = 2, Text = "Average"
                },
                new AnswerViewModel3 {
                    Id = 3, Text = "Good"
                },
            };

            // Get the questions from the database
            // This is a data sample
            var questions = new List <QuestionViewModel3>
            {
                // Rdio button input
                new QuestionViewModel3
                {
                    Id   = 1,
                    Text = "Question 1",
                    QuestionInputType = QuestionInputType.RadioButton,
                    PossibleAnswers   = possibleAnswers,
                },

                // Textbox input
                new QuestionViewModel3
                {
                    Id   = 2,
                    Text = "Question 2",
                    QuestionInputType = QuestionInputType.TextBox,
                },

                // Text Area input
                new QuestionViewModel3
                {
                    Id   = 3,
                    Text = "Question 3",
                    QuestionInputType = QuestionInputType.TextArea,
                }
            };

            // Create a new view model
            var model = new SurveyViewModel3();

            // Copy the questions from the data model into the view model
            foreach (var item in questions)
            {
                model.Questions.Add(item);
            }

            return(View(model));
        }
示例#2
0
        public ActionResult Index(SurveyViewModel3 model)
        {
            if (ModelState.IsValid)
            {
                // Save the questions with selected answers from the VM to your database
                foreach (var question in model.Questions)
                {
                    // question.Id; //to get the question id;
                    // question.SelectedAnswer; // to get radio box answer
                    // question.input; // to get the textbox value; if number use Parse or TryParse
                    // questions.QuestionInputType; //to get the type input: radio, textbox, textarea
                }

                // Return to to your home
                return(RedirectToAction("Index", "Home"));
            }

            // Issue with the model
            return(View(model));
        }