Example #1
0
        // Topic test result details list.
        public async Task <ActionResult> TopicTestResultDetails(int?id)
        {
            try
            {
                // I.Checks.
                // Check id.
                if (!int.TryParse(id.ToString(), out int intId))
                {
                    return(RedirectToAction("Index"));
                }
                // Get TestResultDTO object.
                TestResultDTO testResultDTO = await TestResultService.GetAsync(intId);

                if (testResultDTO == null)
                {
                    return(RedirectToAction("Index"));
                }

                // II. AutoMapper Setup.
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <TestResultDTO, TestResultViewModel>()
                    .ForMember("Id", opt => opt.MapFrom(tr => tr.TestResultId))
                    .ForMember("TestTitle", opt => opt.MapFrom(tr => tr.TestResultDetails.FirstOrDefault().Question.Topic.TopicTitle))
                    .ForMember(tr => tr.UserEmail, option => option.Ignore());
                    cfg.CreateMap <TestResultDetailDTO, TestResultDetailViewModel>()
                    .ForMember("Question", opt => opt.MapFrom(trd => trd.Question.QuestionText))
                    .ForMember("Topic", opt => opt.MapFrom(trd => trd.Question.Topic.TopicTitle));
                });
                IMapper iMapper = config.CreateMapper();

                // III. Set ViewBag properties.
                TestResultViewModel testResult = iMapper.Map <TestResultDTO, TestResultViewModel>(testResultDTO);
                ViewBag.CourseName = testResultDTO.TestResultDetails.FirstOrDefault().Question.Topic.Course.CourseTitle;
                ViewBag.TopicName  = testResultDTO.TestResultDetails.FirstOrDefault().Question.Topic.TopicTitle;
                ViewBag.Result     = testResult.Result * 1000 / testResult.MaxScore;
                ViewBag.MaxScore   = 1000;
                ViewBag.ParentId   = testResultDTO.TestResultDetails.FirstOrDefault().Question.Topic.CourseId;

                // IV. Get data for a view.
                IEnumerable <TestResultDetailDTO>       source = TestResultDetailService.Find(trd => trd.TestResultId == intId);//.ToList();
                IEnumerable <TestResultDetailViewModel> testResultDetailList = iMapper.Map <IEnumerable <TestResultDetailDTO>, IEnumerable <TestResultDetailViewModel> >(source).OrderBy(trd => trd.Topic);

                // V.
                return(View(testResultDetailList));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }