public static void LoadSubjectOptions(this RQIndexViewModel model, IEnumerable <Subject> subjects, string emptyText = "")
        {
            var options = subjects.Select(item => item.ToOption()).ToList();

            if (!String.IsNullOrEmpty(emptyText))
            {
                options.Insert(0, new BaseOption <int>(-1, emptyText));
            }

            model.SubjectOptions = options;
        }
示例#2
0
        public async Task <ActionResult> Index(int mode, int recruit)
        {
            var yearRecruits = _dataService.FetchYearRecruits();

            RQMode selectMode = mode.ToRQModeType();

            if (selectMode == RQMode.Unknown)
            {
                //初次載入頁面
                var rqIndexModel = new RQIndexViewModel();
                rqIndexModel.LoadModeOptions();

                rqIndexModel.YearRecruits = yearRecruits.ToList();

                //考試科目
                var examSubjects = await _subjectsService.FetchExamSubjectsAsync();

                rqIndexModel.LoadSubjectOptions(examSubjects);

                return(Ok(rqIndexModel));
            }


            if (selectMode != RQMode.Read)
            {
                ModelState.AddModelError("RQMode", "僅支援閱讀模式");
                return(BadRequest(ModelState));
            }

            var recruitsViews = yearRecruits.SelectMany(item => item.SubItems);

            var selectedRecruitView = recruitsViews.FirstOrDefault(x => x.Id == recruit);

            if (selectedRecruitView == null)
            {
                ModelState.AddModelError("recruit", "年度不存在");
                return(BadRequest(ModelState));
            }

            //取得題目與解析的附件
            var types = new List <PostType> {
                PostType.Option, PostType.Resolve
            };
            var attachments = (await _attachmentsService.FetchByTypesAsync(types)).ToList();

            var model = new RQViewModel();

            var parts = selectedRecruitView.SubItems;

            if (parts.HasItems())
            {
                foreach (var part in parts)
                {
                    var partView = await InitRQPartViewModelAsync(part, attachments);

                    model.Parts.Add(partView);
                }
            }
            else
            {
                var partView = await InitRQPartViewModelAsync(selectedRecruitView, attachments);

                model.Parts.Add(partView);
            }

            model.LoadTitle();

            return(Ok(model));
        }
 public static void LoadModeOptions(this RQIndexViewModel model)
 {
     model.ModeOptions = GetModeOptions();
 }