public async Task <IActionResult> AddDistrict(District district) { if (ModelState.IsValid) { var country = await _countryRepository.GetByIdAsync(district.CountryId); if (country == null) { ModelState.AddModelError(string.Empty, $"The Country {country.CountryName}, is no longer available for ading new districts"); return(View(district)); } var newDistrict = _converterHelper.ToDistrict(district, true); var exists = await _districtRepository.ExistsInCountryAsync(country.Id, newDistrict.DistrictName, newDistrict.Id); if (exists) { ModelState.AddModelError(string.Empty, $"There is allready a District registered with the name {newDistrict.DistrictName}, please insert another"); ViewBag.CountryId = district.CountryId; return(View(district)); } try { await _districtRepository.CreateAsync(newDistrict); return(RedirectToAction($"DetailsCountry/{country.Id}")); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { ModelState.AddModelError(string.Empty, $"There is allready a District registered with the name {newDistrict.DistrictName}, please insert another"); ViewBag.CountryId = district.CountryId; return(View(district)); } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); return(View(district)); } } } ViewBag.CountryId = district.CountryId; return(View(district)); }