public async Task <IActionResult> Add(AddRegionViewModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.Name) && await regionService.ExistsAsync(model.Name))
            {
                ModelState.AddModelError(nameof(model.Name), "Region with the same name already exists.");
            }

            if (!await countryService.ExistsAsync(model.CountryId))
            {
                ModelState.AddModelError(nameof(model.CountryId), $"There is no country with id '{model.CountryId}'");
            }

            if (!ModelState.IsValid)
            {
                model.Countries = await GetCountries();

                return(View(model));
            }

            var id = await regionService.AddAsync(model.Name, model.Description, model.CountryId);

            if (id < 1)
            {
                ModelState.AddModelError(string.Empty, "Save failed.");
                model.Countries = await GetCountries();

                return(View(model));
            }

            return(RedirectToAction(nameof(Add)));
        }
Example #2
0
        public async Task <IHttpActionResult> PostRegion(Region region)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var Region = await _regionService.AddAsync(region);

            return(CreatedAtRoute("ApiRoute", new { id = Region.Id }, region));
        }
        public async Task <IActionResult> Save(RegionDto region)
        {
            var newRegion = await _regionService.AddAsync(_mapper.Map <Region>(region));

            return(Created(string.Empty, _mapper.Map <RegionDto>(newRegion)));
        }