How do I pass a View value to the Controller in ASP.NET core?

1

I want to move from View

<table style="width:100%">
<tr>
    <th>Serviço</th>
    <th>Data</th>
    <th>Feito</th>
</tr>
@{
    foreach (var item in ViewBag.servico)
    {
<tr>
    <td>@item.Servico</td>
    <td>@item.Data</td>
    <td>@item.Feito</td>
    <td>Editar</td>
    <td>
        <form action="/Tarefas/Remover" method="post">
            @{
                var tarefa = item;
             }
            <input type="submit" value="Excluir" />
        </form>
    </td>
</tr>
    }
}

For the controller:

[HttpPost]
public IActionResult Remover(Tarefa tarefa)
{
    using (var item = new AgendaDBContext())
    {
        item.Servicos.Remove(tarefa);
        item.SaveChanges();

    }
    return View("Lista");
}
    
asked by anonymous 14.09.2018 / 00:25

1 answer

0

SOLVED * I'm just wondering if this would be a bad practice, I am sending the id to controller creating a new task instance and associating it with an id and sent to the entity to delete.

Creating a new instance is a bad practice?

<table style="width:100%">
<tr>
    <th>Serviço</th>
    <th>Data</th>
    <th>Feito</th>
</tr>
@{
    foreach (var item in ViewBag.servico)
    {
<tr>
    <td>@item.Servico</td>
    <td>@item.Data</td>
    <td>@item.Feito</td>
    <td>Editar</td>
    <td>
        <form action="/Tarefas/Remover" method="post">
                <input type="hidden" name="id" value="@item.Id" />
            <input type="submit" value="Excluir" />
        </form>
    </td>
</tr>
            }
}

Controller:

[HttpPost]
    public IActionResult Remover(int id)
    {            
        using (var item = new AgendaDBContext())
        {
            Tarefa tarefa = new Tarefa();
            tarefa.Id = id;
            item.Servicos.Remove(tarefa);
            item.SaveChanges();

        }
        return RedirectToAction("Lista", "Tarefas");
    }
    
15.09.2018 / 15:50