How to create a controller with data from another table in ASP.NET?

0

I'm not sure how to properly create an Employee Controller. The Official class is linked to three others, being CBO, Company and Department.

So, in the database schema, they receive these three primary keys. My big problem then is that I'm not sure how to create the Controller for Create, Edit, Detail, and Delete, just the Index I was able to do.

As well, I'm having problems creating Views, where the View of the Create works, but after I click save, it returns to the Index and does not persist, not saving. I do not 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();
        }
    }
}
    
asked by anonymous 14.06.2018 / 09:45

1 answer

0

Look I recommend you create your actions by receiving objects or instead of using formcollection.

Read: link

I think this will help you.

On your error of not persisting, debug and see if your method! OfficialDeO.Persistir (t) is saving? Because the problem must be in it, some kind of data is not beating!

    
14.06.2018 / 15:13