public async Task <IActionResult> DeleteDistrict(int id) { var district = await _districtRepository.GetByIdAsync(id); try { await _districtRepository.DeleteAsync(district); } catch (Exception ex) { if (ex.InnerException.Message.Contains("REFERENCE constraint")) { if (ModelState.IsValid) { //TODO make buttons to get to the respective views ViewBag.Error = $"There are counties associated with the District named {district.DistrictName}, either delete them or deactivate country"; return(View(district)); } } else { ViewBag.Error = ex.InnerException.Message; return(View(district)); } } var country = await _countryRepository.GetByIdAsync(district.CountryId); if (country != null) { return(RedirectToAction($"DetailsCountry/{country.Id}")); } else { return(RedirectToAction(nameof(Index))); } }