private async Task <ActionResult> ProcessEstablishmentsSearch(EstablishmentSearchViewModel model, EstablishmentSearchPayload payload) { if (model.HasError) { return(NoResults(model)); } else { PopulateSelectList(payload); // select only fields we use in this context var results = await _establishmentReadService.SearchAsync(payload, User); if (payload.Skip == 0) { model.Count = results.Count; } model.Results = results.Items; if (results.Count == 0) { return(NoResults(model)); } var localAuthorities = await _lookupService.LocalAuthorityGetAllAsync(); foreach (var item in model.Results) { model.Addresses.Add(item, await item.GetAddressAsync(_lookupService)); var laEstab = string.Empty; if (item.LocalAuthorityId.HasValue && item.EstablishmentNumber.HasValue) { var code = localAuthorities.FirstOrDefault(x => x.Id == item.LocalAuthorityId)?.Code; if (code != null) { laEstab = string.Concat(code, "/", item.EstablishmentNumber?.ToString("D4")); } } model.LAESTABs.Add(item, laEstab); } } if (model.Count == 1 && model.GoToDetailPageOnOneResult) { return(RedirectToEstabDetail(model.Results.First().Urn.GetValueOrDefault())); } else { var permittedStatusIds = await _establishmentReadService.GetPermittedStatusIdsAsync(User); var establishmentTypes = await _lookupService.EstablishmentTypesGetAllAsync(); var establishmentGroupTypes = await _lookupService.EstablishmentTypeGroupsGetAllAsync(); model.EstablishmentTypes = establishmentGroupTypes.Select(groupType => new HeirarchicalLookupItemViewModel { Id = groupType.Id, Name = groupType.Name, ChildItems = establishmentTypes.Where(c => c.GroupIds.Contains(groupType.Id)) .Select(e => new HeirarchicalLookupItemViewModel { Id = e.Id, Name = e.Name }) .ToList() }) .ToList(); model.EstablishmentTypeLookup = establishmentTypes.ToDictionary(e => e.Id, e => e.Name); model.EstablishmentStatuses = (await _lookupService.EstablishmentStatusesGetAllAsync()) .Where(x => permittedStatusIds == null || permittedStatusIds.Contains(x.Id)) .Select(x => new LookupItemViewModel(x)); model.EducationPhases = (await _lookupService.EducationPhasesGetAllAsync()).Select(x => new LookupItemViewModel(x)); model.ReligiousCharacters = (await _lookupService.ReligiousCharactersGetAllAsync()).Select(x => new LookupItemViewModel(x)); await PopulateLookups(model); return(View("Index", model)); } }