public IActionResult CreateProjectType() { // get all features from the Db var featuresFromRepo = featureUnitOfWorkRepository.featureRepository.GetAllFeatures(); // this list of Select List items will be displayed. User will choose which ones to add var selectList = new List <SelectListItem>(); // populate select list items with entries retrieved from the Db foreach (var item in featuresFromRepo) { selectList.Add(new SelectListItem(item.FeatureText, item.FeatureId.ToString())); } var vm = new CreateProjectTypeViewModel() { Features = selectList }; return(PartialView(vm)); }
public async Task <IActionResult> CreateProjectType(CreateProjectTypeViewModel vm) { try { // get all features from the Db var featuresFromRepo = featureUnitOfWorkRepository.featureRepository.GetAllFeatures(); // this list of Select List items will be displayed. User will choose which ones to add var selectList = new List <SelectListItem>(); // populate select list items with entries retrieved from the Db foreach (var item in featuresFromRepo) { selectList.Add(new SelectListItem(item.FeatureText, item.FeatureId.ToString())); } vm.Features = selectList; if (!ModelState.IsValid) { return(View(vm)); } ProjectType projectType = new ProjectType() { StarQuality = vm.StarQuality, DaysToDeliver = vm.DaysToDeliver, Rate = vm.Rate, ProjectTypeName = vm.ProjectTypeName }; foreach (var item in vm.SelectedTags) { if (!Int32.TryParse(item, out int result)) { ModelState.AddModelError(string.Empty, "Someone might've interfered with the inspect element :))"); return(View(vm)); } projectType.ProjectTypeFeatures.Add(new ProjectTypeFeature() { FeatureId = result }); } await featureUnitOfWorkRepository.projectTypeRepository.Add(projectType); featureUnitOfWorkRepository.Save(); actionFeedback.SuccessUnsuccess = true; return(RedirectToAction("Index")); } catch (NullReferenceException ex) { if (vm.SelectedTags == null) { ModelState.AddModelError(string.Empty, "Please select at least one feature to add."); } logger.LogError(ex, "Null reference during Project Type creation. Most likely, no featured selected. Exception message: " + ex.Message); actionFeedback.SuccessUnsuccess = false; return(View(vm)); } }