public IActionResult Salvar(GftViewModel gftVM) { try { if (ModelState.IsValid) { if (gftVM == null) { throw new Exception("Gft inválida!"); } _enderecoRepository.Salvar(gftVM.Endereco); _gftRepository.Salvar(gftVM.ToModel()); return(RedirectToAction("Index")); } else { return(View("Gft", gftVM)); } } catch (EntidadeNaoEncontradaException) { return(NotFound("Cargo não encontrado")); } catch (Exception ex) { return(StatusCode(500, ex)); } }
public static Gft ToModel(this GftViewModel gftVM) { var gft = new Gft() { id = (gftVM.Id.HasValue ? gftVM.Id.Value : 0), nome = gftVM.Nome, endereco = gftVM.Endereco, telefone = gftVM.Telefone, status = true }; if (gftVM.HasId) { gft.data_alteracao = DateTime.Now; } return(gft); }