public async Task <IActionResult> Create(Specie specie)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _specieRepository.CreateAsync(specie);

                    return(RedirectToAction(nameof(Index)));
                }
                catch (DbUpdateException dbUpdateException)
                {
                    if (dbUpdateException.InnerException.Message.Contains("duplicate"))
                    {
                        ModelState.AddModelError(string.Empty, "There are a Specie with the same name.");
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, dbUpdateException.InnerException.Message);
                    }
                }
                catch (Exception exception)
                {
                    ModelState.AddModelError(string.Empty, exception.Message);
                }
            }

            return(View(specie));
        }
示例#2
0
        public async Task <IActionResult> Create(CreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                var specie = _converterHelper.ToSpecieEntity(model);
                await _specieRepository.CreateAsync(specie);

                return(RedirectToAction(nameof(Index)));
            }

            return(View(model));
        }