Ejemplo n.º 1
0
        public async Task <IActionResult> CreateTank(TankCreateViewModel model)
        {
            if (ModelState.IsValid && model.Tank != null)
            {
                if (await _repository.TankNameExistsAsync(model.Tank.Name))
                {
                    ModelState.AddModelError("Name exists", _stringLocalizer["A tank with that name already exists"]);
                }
                else
                {
                    Tank tankModel = model.Tank;
                    tankModel.LastModifiedByName = User.Identity.Name;
                    int result = await _repository.AddTankAsync(tankModel);

                    if (result < 1)
                    {
                        TempData["TankMessage"] = _stringLocalizer["Failed to create"].ToString();
                    }
                    else
                    {
                        TempData["TankMessage"] = _stringLocalizer["Tank created successfully"].ToString();
                    }
                    return(RedirectToAction(nameof(Index)));
                }
            }
            model.Sections = await _repository.GetSectionsAsync();

            ViewBag.Title = _stringLocalizer["Create Tank"].ToString();
            return(View(model));
        }