Change the value of a checkbox id MVC4

0

I have a problem with my view checkboxes, and I can not understand how I can take all the values that are selected and send them to the controller to be able to operate with them.

I have the following: Vista.

@model List<Model.ofertas_contactos>

@{
    ViewBag.Title = "Listado general de solicitudes";
    Layout = "~/Views/Shared/_Layout.cshtml";

}

<head>
    <meta http-equiv="refresh" content="20">
</head>
@* Empieza la vista *@
<body style=" background-color:#353435">

    <h1 style="color:#fff; padding-top:30px" align="center">Listado General de Solicitudes Pendientes</h1>
    <br />
    <table align="center" class="table-striped, text-center, table-condensed" style="font-size:20px" cellpadding="20">
        @* Cabecera *@
        <thead>
            <tr class="text-center" style="color:#e98300;font-family:'Trebuchet MS'">
                <th class="text-center" ; bgcolor="black"><input type="checkbox" name="chkAll" value="All" id="selecctall" /></th>
                <td class="text-center" ; bgcolor="black">Hora solicitud</td>
                <td class="text-center" ; bgcolor=" black">Capturar</td>
            </tr>
        </thead>
        <tbody>
               foreach (Filtro)
                {

                   <tr style="color:white ;font-family:'Trebuchet MS'">
                        <td class="text-center"><input type="checkbox" name="sid" value="@usu.idContacto" class="checkbox1" /></td>
                        <td>@usu.fechaHora</td>
                        <td class="text-center"><a class=" btn btn-primary btn-lg" href="~/home/capturar/@usu.idContacto" title="Capturar"></a></td>
                    </tr>

                }


        </tbody>
    </table>
    @*Boton*@
    <a class="btn btn-primary" href="ruta/cambiarCampo" title="Cambiar Campo" style="margin-left:473px; margin-top:30px">Cambiar Campo</a>
</body>

And in the HomeController I have the following:

public ActionResult cambiarCampo(int[] id)
    {
        return View();
    }
    
asked by Zarios 11.07.2016 в 11:32
source

1 answer

2

Remember that what you must match with the controller parameter is the name of the tag in html

In your case the checkbox has the name="sid" so the parameter should have the same name

public ActionResult cambiarCampo(int[] sid)
{
    return View();
}

What I do not see in the code is that you make a POST to the server to send this data, if you only use the link you would be sending a single value for GET in the url.

You should define the code within a

@using(Html.BeginForm("cambiarCampo")){

    //aqui defines html con los checkbox     

}

and in the action you defend the method that receives the data in the POST when making the submit

[HttpPost]
public ActionResult cambiarCampo(int[] sid)
{
    return View();
}
    
answered by 11.07.2016 / 15:35
source