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

Asked

Viewed 426 times

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] :)

1 answer

0

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

Da a lida em: https://docs.microsoft.com/pt-br/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

I think this will help you.

About your error of not persisting, debug and see if your method ! Worksiodao.Persist(t) is saving? 'Cause the problem must be in him, some kind of dice ain’t hitting!

  • Hey, Ivan. I kind of get it. This Collection is a standard that my teacher requested to use, unfortunately I do not understand much of the language yet and so I used. Hehe About the method ! Functionto.Persist(t), is not saving no. Although I did the test in Unitteste and persisted normal. I tweaked, took out the IF with the ! and did it in a common way, still yes persists the error. I don’t know what else to do, and now I’m still wondering if my syntax on the Try part is correct... :\

  • Surely the error must be in your DAO

  • Like, the DAOS, I reviewed, then I ran the tests by Unit and they typically persist data, search all, search for ID and update. I was told that some data is not declared correctly in the Controller, but I don’t know how to fix it. :\

  • In the fields department, cbo and company, passes only the ID..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.