// Subject specialities list. public async Task <ActionResult> SubjectSpecialities(int?id) { // Check id. if (!int.TryParse(id.ToString(), out int intId)) { return(RedirectToAction("Index")); } // Get SubjectDTO. SubjectDTO subjectDTO = await SubjectService.GetAsync(intId); if (subjectDTO == null) { return(RedirectToAction("Index")); } // Set ViewBag properties. ViewBag.ParentId = intId; ViewBag.SubjectName = subjectDTO.SubjectName; // AutoMapper Setup. var config = new MapperConfiguration(cfg => { cfg.CreateMap <SpecialityDTO, SpecialityViewModel>() .ForMember("Id", opt => opt.MapFrom(obj => obj.SpecialityId)) .ForMember("Name", opt => opt.MapFrom(obj => obj.SpecialityName)); }); IMapper iMapper = config.CreateMapper(); List <SpecialityDTO> source = SpecialityService.Find(dto => dto.SubjectId == intId).OrderBy(o => o.SpecialityName).OrderBy(o => o.IsApproved).ToList(); IEnumerable <SpecialityViewModel> specialityOrderedList = iMapper.Map <IEnumerable <SpecialityDTO>, IEnumerable <SpecialityViewModel> >(source); return(View(specialityOrderedList)); }
// Subject specialities list. public async Task <ActionResult> SubjectSpecialities(int?id) { try { // I. Checks. // Get Id for the current user. string currentUserId = System.Web.HttpContext.Current.User.Identity.GetUserId(); if (currentUserId == null) { return(RedirectToAction("Login", "Account")); } // Check id. if (!int.TryParse(id.ToString(), out int intId)) { return(RedirectToAction("Index")); } // Get SubjectDTO. SubjectDTO subjectDTO = await SubjectService.GetAsync(intId); if (subjectDTO == null) { return(RedirectToAction("Index")); } // II. Set ViewBag properties. PLRepository.SetViewBagProperiesForModeratorSubscription(UserService, currentUserId, SubscriptionForModeratorService, out bool?viewBagIsAllowedToSuggest, out bool?viewBagGoToTrialSubscription, out bool?viewBagGoToSubscription); ViewBag.IsAllowedToSuggest = viewBagIsAllowedToSuggest; ViewBag.GoToTrialSubscription = viewBagGoToTrialSubscription; ViewBag.GoToSubscription = viewBagGoToSubscription; ViewBag.ParentId = intId; ViewBag.SubjectName = subjectDTO.SubjectName; // III. AutoMapper Setup. var config = new MapperConfiguration(cfg => { cfg.CreateMap <SpecialityDTO, SpecialityViewModel>() .ForMember("Id", opt => opt.MapFrom(obj => obj.SpecialityId)) .ForMember("Name", opt => opt.MapFrom(obj => obj.SpecialityName)); }); IMapper iMapper = config.CreateMapper(); // IV. Get data for a view. List <SpecialityDTO> source = SpecialityService.Find(dto => dto.SubjectId == intId && dto.IsApproved).OrderBy(o => o.SpecialityName).ToList(); IEnumerable <SpecialityViewModel> specialityOrderedList = iMapper.Map <IEnumerable <SpecialityDTO>, IEnumerable <SpecialityViewModel> >(source); // V. return(View(specialityOrderedList)); } catch (Exception ex) { throw new Exception(ex.Message); } }