I need to send 2 values to the controller, one of the values is fixed and the other is a dynamic value that is fetched from a ViewModel
.
The dynamic value is displayed in a table, and it is an input, of type submit
, which if I click on it, both the fixed and dynamic values try to send them to a controller method, but they do not seem to be arriving. I wanted to know if you could help me send them and that they arrive.
This is my view:
<tbody>
<form action="~/Home/LeerOpcion" method="post">
<label hidden value="entrega" name="seleccion"></label>
@foreach (var item in Model)
{
<tr>
<td><button type="submit" class="btn btn-link" value="@item.NumeroEntrega" name="identi">@item.NumeroEntrega</button></td>
<td>@item.NumeroPedido</td>
<td>@item.NumeroRemito</td>
<td>@item.CantFotos</td>
<td>@item.Fecha</td>
<td>@item.Hora</td>
</tr>
}
</form>
</tbody>
And this is my controller method:
public IActionResult LeerOpcion(string seleccion, string identi)
{
List<PedidoViewModel> listaPedidos;
try
{
identi = CleanString(identi);
if (identi == null || identi == "")
{
return View("Index");
}
switch (seleccion)
{
case "pedido":
listaPedidos = VerPedido(identi);
return View("Pedido", listaPedidos);
case "remito":
listaPedidos = VerRemito(identi);
return View("Pedido", listaPedidos);
case "entrega":
listaPedidos = VerEntrega(identi);
return View("Pedido", listaPedidos);
}
return View();
}
catch (Exception ex)
{
ViewBag.error = ex.Message;
return View("Error");
}
From the view I want to send the value of the label (name="selection", value="delivery") and the input (name="identi", value="@item.NumeroDelivery").
I tried with the form, with the @using(Html.Beginform)
... with a [email protected]("LeerOpcion", "Home", new{seleccion = "entrega", identi = @item.NumeroEntrega})
in the input
/button
Can someone help me please?
What happens is that you have many misconceptions. To send something you must put it in an input.
If you want it to be hidden, you can put:
You also have a Form and inside it you have a foreach. When you do this, the submit will send all the fields inside the form, when I understand that you want to send only the selected one. (if you want to send everything, the controller must receive an array)
Then your controller sends back a view, so I understand that the act of pressing a button changes the page, so you should put something like this:
and the controller