0

I'm trying to post data to a Controller from an MVC View and I'm having problems. The code is as follows:

   <div id="no-more-tables">
    <table id="CarteraClients"  style="width: 100%;">
        <thead>
            <tr>
                <th scope="col" id="col1" width="55%">Cliente</th>
                <th scope="col" id="col2">Semana 1 </th>              
                <th scope="col" id="col2">Semana 2 </th>
                <th scope="col" id="col2">Semana 3 </th>  
                <th scope="col" id="col2">Semana 4 </th>
                <th scope="col" id="col2">Semana 5 </th>
            </tr>
        </thead>
        <tbody>
            @{ 
                if (Model.Count() == 0)
                {
                    <tr>
                        <th colspan="4" class="error text-center">
      <small class="error">No hay elementos coincidentes.</small></th>
                    </tr>
                }
                else
                {
                    foreach (var item in Model)
                    { 
                        <tr>                 
                            <td data-title="Razon Social">
                                @Html.DisplayFor(modelItem => 
   item.razon_social)
                            </td>
                            <td data-title="Semana 1" id="Semana1" 
        class="row1">@Html.CheckBox("chkSemana1", false)</td>
                            <td data-title="Semana 2" id="Semana2" 
       class="row1">@Html.CheckBox("chkSemana2", false)</td>                           
                            <td data-title="Semana 3" id="Semana3" 
      class="row1">@Html.CheckBox("chkSemana3", false)</td>
                            <td data-title="Semana 4" id="Semana4" 
      class="row1">@Html.CheckBox("chkSemana4", false)</td>
                            <td data-title="Semana 5" id="Semana5" 
      class="row1">@Html.CheckBox("chkSemana5", false)</td>
                       </tr>             
                    }
                }
            }
        </tbody>
       </table>
      <button type="button" class="small" onclick="llenardata()"><img 
      src="~/Content/img/magnifying-glass-8x.png" alt="Buscar" width="16" 
     height="16" /> Guardar cartera</button>
       </div>

The script that I'm using to generate the structure to send via json:

   <script type="text/javascript">
    function llenardata() {
        var cant = 0;
        var tabla = document.getElementById("CarteraClients");
        var rowLength = 
 document.getElementById("CarteraClients").rows[0].cells.length;
        var tablaresultados = [];
        for (i = 0; i < tabla.rows.length; i++) {        
            var ocells = tabla.rows.item(i).cells;
            var cellLength = ocells.length;
                var cliente = ocells.item(0).innerText;
                var semana1 = ocells.item(1).innerText;
                var semana2 = ocells.item(2).innerText;
                var semana3 = ocells.item(3).innerText;
                var semana4 = ocells.item(4).innerText;
                var semana5 = ocells.item(5).innerText;

                tablaresultados.push(cliente);
                //tablaresultados.push(semana1);
                //tablaresultados.push(semana2);
                //tablaresultados.push(semana3);
                //tablaresultados.push(semana4);
                //tablaresultados.push(semana5);
        }
        console.log(tablaresultados);
        $.ajax({
            url: url("IngresoProyeccion") + 'GuardarCarteraCliente',
            data:
                'IdUser=' + delegado +
                '&Ano=' + anyos +
                '&Mes=' + meses +
                '&Oportunidad= Levantamiento de Oportunidades'+  
                '&Cartera='+tablaresultados,

            dataType: 'json',
            type: 'POST',
        }).success(function (html) {
            DesbloquearPantalla();
            $("#divReporte").html(html);
            //$("body").animate({ scrollTop: $(document).height() }, 
  'fast');
        });
    }
</script>

But it doesn't take the values of the checkbox on semana1, semana2, semana3, semana4, semana5 in the var tablaresultados.

I need help to resolve this problem. Thanks.

RickL
  • 3,318
  • 10
  • 38
  • 39
  • 1
    Why dont you try the @Html.CheckBoxFor() method? Check this question for more information: https://stackoverflow.com/questions/12674572/proper-usage-of-net-mvc-html-checkboxfor and here is the documentation for the method, https://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.checkboxfor(v=vs.118).aspxhttps://msdn.microsoft.com/en-us/library/system.web.mvc.html.inputextensions.checkboxfor(v=vs.118).aspx – Master Yoda Jun 30 '17 at 15:53
  • kindly check this link it might help you to take the value from checkbox:https://stackoverflow.com/a/14429375/3397630 – Karthik Elumalai Jul 01 '17 at 02:05

0 Answers0