Exemplo n.º 1
0
        public IActionResult SaveOrEdit(VeiculoModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            try
            {
                if (model.ID == 0)
                {
                    _veiculoApplication.Adicionar(model);

                    if (model.ID > 0)
                    {
                        return(RedirectToAction("index"));
                    }
                    else if (model.MensagemValidacao != null && model.MensagemValidacao.Count > 0)
                    {
                        foreach (var item in model.MensagemValidacao)
                        {
                            string message = string.Format("Atenção: {0}", item);
                            ModelState.AddModelError(string.Empty, message);
                        }
                    }
                }
                else
                {
                    var veiculoEntity = _veiculoApplication.ObterPorId(model.ID);

                    _veiculoApplication.Atualizar(model);

                    if (veiculoEntity.ID > 0)
                    {
                        return(RedirectToAction("index"));
                    }
                }
            }
            catch (Exception ex)
            {
                string message = string.Format("Atenção: {0}", ex.Message);
                ModelState.AddModelError(string.Empty, message);
            }

            return(View(model));
        }