//cette methode prend en parametre le filtre de l'utilisateur et retourne le model associé private CreateSurveyViewModel BuildSurveyViewModel(SurveyFilterInfoViewModel surveyFilterInfo = null) { var model = surveyFilterInfo == null ? new CreateSurveyViewModel() { CategoryId = CATEGORY_ID, DifficultyId = DIFFICULTY_ID, TypeId = TYPE_ID } : new CreateSurveyViewModel() { CategoryId = surveyFilterInfo.CategoryId, DifficultyId = surveyFilterInfo.DifficultyId, TypeId = surveyFilterInfo.TypeId }; //dans le selectlistItem nous avons model.Categories = _categoryDao.GetAllQuestionCategory().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem { Text = o.Name, Value = o.Id.ToString(), // Put default categoryId qs const Selected = o.Id == model.CategoryId }); model.Difficulties = _difficultyDao.GetAll().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem { Text = o.DifficultyLevel, Value = o.Id.ToString(), Selected = o.Id == model.DifficultyId }); model.QuestionTypes = _questionTypeDao.GetAll().Select(o => new Microsoft.AspNetCore.Mvc.Rendering.SelectListItem { Text = o.Name, Value = o.Id.ToString(), Selected = o.Id == model.TypeId }); //on recupère les questions depuis la base de donnée en fonction des différents paramètres var questions = _questionDao.GetAll(); var questionListViewModels = new List <QuestionViewModel>(); if (questions != null && questions.Any()) { foreach (var question in questions.Where(q => surveyFilterInfo == null || ( (surveyFilterInfo.CategoryId == 0 || q.Category.Id == surveyFilterInfo.CategoryId) && (surveyFilterInfo.TypeId == 0 || q.Type.Id == surveyFilterInfo.TypeId) && (surveyFilterInfo.DifficultyId == 0 || q.Difficulty.Id == surveyFilterInfo.DifficultyId) ))) { questionListViewModels.Add( new QuestionViewModel() { Id = question.Id, CategoryName = question.Category.Name, TypeName = question.Type.Name, DifficultyLevel = question.Difficulty.DifficultyLevel, Content = question.Content, IsChecked = surveyFilterInfo != null && surveyFilterInfo.SurveySelectedQuestions != null && surveyFilterInfo.SurveySelectedQuestions.Any() && surveyFilterInfo.SurveySelectedQuestions.Any(sq => sq.Id == question.Id && sq.IsChecked) }); } } model.SurveySelectedQuestions = questionListViewModels; return(model); }
public IActionResult FilterQuestions(SurveyFilterInfoViewModel surveyFilterInfo) { var model = BuildSurveyViewModel(surveyFilterInfo); return(View("Create", model)); }