public ActionResult AddEdit(EmpresaVM empresa) { if (ModelState.IsValid) { // No id so we add it to database if (empresa.ID <= 0) { _db.Empresas.Add(new Empresa { Nome = empresa.RazaoSocial, NomeFantasia = empresa.NomeFantasia, CNPJ = empresa.CNPJ, WebSite = empresa.WebSite, }); /*faltou implementar o add e edit do post do obj contato * ficaria mais ou menos assim: */ //empresa.Contatos.ToList().ForEach(d => // _db.Contatos.Add(new Contato // { // Nome = d.Nome, // Telefone = d.Telefone, // Celular = d.Celular, // Email = d.Email // }) //); } else { var edit = _db.Empresas.Find(empresa.ID); edit.Nome = empresa.RazaoSocial; edit.NomeFantasia = empresa.NomeFantasia; edit.CNPJ = empresa.CNPJ; edit.WebSite = empresa.WebSite; //descobrir o bloco do cara e mandá-lo para a interface var posicao = _db.Empresas.Where(d => d.ID <= empresa.ID).OrderBy(d => d.ID).Count(); TempData["bloco"] = (posicao / qtdeItensNoBloco).ToString(); } _db.SaveChanges(); TempData["update"] = true; return RedirectToAction("Index"); } return View(empresa); }
public ActionResult Edit(int id) { var find = _db.Empresas.Find(id); if (find == null) throw new Exception("Empresa não existe!"); var empresa = new EmpresaVM { ID = find.ID, RazaoSocial = find.Nome, NomeFantasia = find.NomeFantasia, CNPJ = find.CNPJ, WebSite = find.WebSite, Contatos = find.Contatos.Select(d => new ContatoVM { ID = d.ID, Nome = d.Nome, Telefone = d.Telefone, Celular = d.Celular, Email = d.Email }).ToList() }; return View("AddEdit", empresa); }