0
I don’t know how to properly create an Employee Controller. The Employee class is linked to three others, being CBO, Company and Department.
So in the database schema, they get these three primary keys. My big problem then is that I don’t know how to create the Controller to Create, Edit, Detail and Delete, only the Index I was able to do.
As well, I’m having problems creating the Views, where the Create View works, but after I click save, goes back to Employee Index and does not persist, not saving. I don’t know what else to do!
Here is the Controller:
public class FuncionarioController : Controller
{
// GET: Unidade
public ActionResult Index()
{
return View(FuncionarioDAO.BuscarTodos());
}
// GET: Unidade/Details/5
public ActionResult Details(int id)
{
Funcionario i = FuncionarioDAO.BuscarPorId(id);
if (i == null)
{
return HttpNotFound();
}
return View(i);
}
// GET: Unidade/Create
public ActionResult Create()
{
ViewBag.empresas = EmpresaDAO.BuscarTodos();
ViewBag.departamentos = DepartamentoDAO.BuscarTodos();
ViewBag.cbos = CboDAO.BuscarTodos();
return View();
}
// POST: Unidade/Create
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
Funcionario t = new Funcionario();
t.Nome = Convert.ToString(collection["Nome"]);
t.Matricula = Convert.ToString(collection["Matricula"]);
t.Ctps = Convert.ToString(collection["Ctps"]);
t.Endereco = Convert.ToString(collection["Endereco"]);
t.Empresa = EmpresaDAO.BuscarPorId(Convert.ToInt32(collection["Empresa.Id"]));
t.Departamento = DepartamentoDAO.BuscarPorId(Convert.ToInt64(collection["Departamento.Id"]));
t.Cbo = CboDAO.BuscarPorId(Convert.ToInt64(collection["Cbo.Id"]));
if (!FuncionarioDAO.Persistir(t))
{
return View();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Unidade/Edit/5
public ActionResult Edit(int id)
{
Funcionario i = FuncionarioDAO.BuscarPorId(id);
if (i == null)
{
return HttpNotFound();
}
return View(i);
}
// POST: Unidade/Edit/5
[HttpPost]
public ActionResult Edit(int id, FormCollection collection)
{
try
{
// TODO: Add update logic here
Funcionario i = new Funcionario();
i.Id = id;
i.Nome = collection["Nome"];
i.Matricula = collection["Matricula"];
i.Ctps = collection["Ctps"];
i.Endereco = collection["Endereco"];
i.Empresa = EmpresaDAO.BuscarPorId(Convert.ToInt32(collection["Empresa.Id"]));
i.Departamento = DepartamentoDAO.BuscarPorId(Convert.ToInt64(collection["Departamento.Id"]));
i.Cbo = CboDAO.BuscarPorId(Convert.ToInt64(collection["Cbo.Id"]));
if (!FuncionarioDAO.Persistir(i))
return View();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
// GET: Unidade/Delete/5
public ActionResult Delete(int id)
{
Funcionario i = FuncionarioDAO.BuscarPorId(id);
if (i == null)
{
return HttpNotFound();
}
return View(i);
}
// POST: Unidade/Delete/5
[HttpPost]
public ActionResult Delete(int id, FormCollection collection)
{
try
{
FuncionarioDAO.Excluir(id);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
Welcome to Stackoverflow. I edited your question to remove the greetings, etc.. because we usually keep the text as clean as possible to focus on your question about programming. Do the [tour] to learn how the community, if you need help, go to [help]. If you have questions about the operation, rules and procedures of the site visit [meta] :)
– NoobSaibot