Пример #1
0
        public IActionResult Index()
        {
            // seed 'Results' db if no data exists
            //var result1 = _seedService.SeedSurveyMaster(10); //craate 10 surveys
            //var result2 = _seedService.SeedAnswerGroup(5); //create 5 groups of answers ... each containing x options
            //var result3 = _seedService.SeedResult();

            var sessionId = string.Empty;

            if (TempData["SessionId"] == null)
            {
                sessionId = Guid.NewGuid().ToString();

                // store the session ID in TempData
                TempData["SessionId"] = sessionId;
            }

            var questionModel = _questions.GetAllByLevel(1);
            var q             = questionModel.Select(q => new QuestionListingModel
            {
                Id       = q.Id,
                Level    = q.Level,
                Order    = q.Order,
                Question = q.Question
            }).ToList();

            // THIS IS A HACK ??
            // pre-load the survey (answer) model...
            foreach (var row in q)
            {
                var question = _questions.Get(row.Id);
                var newRow   = new Survey
                {
                    SessionId = sessionId,
                    QMaster   = question,
                    Answer    = 0
                };
                _answers.Add(newRow);
            }

            // load model for UI ... this was just populated above
            var answerModel = _answers.GetAllBySessionId(sessionId);
            var a           = answerModel.Select(a => new SurveyListingModel
            {
                Id        = a.Id,
                SessionId = a.SessionId,
                QMaster   = a.QMaster,
                Answer    = a.Answer
            }).ToList();

            var answerOptions = _answerOptions.GetAllByKey(1);
            var ao            = answerOptions.Select(a => new AnswerListingModel
            {
                Id     = a.Id,
                Key    = a.Key,
                Val    = a.Val,
                Answer = a.Answer
            }).ToList();

            var model = new TestListingModel
            {
                Questions     = q,
                Answers       = a,
                AnswerOptions = ao
            };

            TempData.Keep();
            return(View(model));
        }