Example #1
0
        // GET: Survey
        public ActionResult Index()
        {
            // Get the questions from the database
            // This is a data sample
            var questions = new List <QuestionViewModel2>
            {
                new QuestionViewModel2 {
                    Id = 1, Text = "Question 1"
                },
                new QuestionViewModel2 {
                    Id = 2, Text = "Question 2"
                },
            };

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

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

            return(View(model));
        }
Example #2
0
        public ActionResult Index(SurveyViewModel2 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.Score; // to get the answer 1, 2, 3; if not set it is null
                }

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

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