cant pass cheked column to controller - server side

cant pass cheked column to controller - server side

lbelalcazarlbelalcazar Posts: 2Questions: 1Answers: 0
edited February 2021 in Free community support

Hi to all, sorry for the post, it's the first, ill try to doit better next time.

I am trying to pass the selected columns to the controller, but I am failing at some point, since if I show it by console if it shows me data but in the controller it does not.

//

 $(document).ready(function () {

            var table = $("#table_id").DataTable({
                "processing": true,
                "serverSide": true,
                "ajax": {
                    "url": "@Url.Content("~/RADICACIONES/Json")",
                    "type": "POST",
                    "datatype": "json"
                },

                "lengthMenu": [[20, 40, 60, 100, -1], [20, 40, 60, 100, "All"]],
                "pageLength": 20,
                "filter": true,
                "pagingType": "full_numbers",
                "responsivePriority": 1,
                "data": null,
                "deferRender": true,
                "scrollY": "60vh",
                "scrollCollapse": true,
                "columns": [
                    { "data": "Fk_Documento", "name": "Fk_Documento", "autoWidth": true },
                    { "data": "UserfullNames", "name": "UserfullNames", "autoWidth": true },               
                ],

                "columnDefs": [
                    { "className": "dt-center", "targets": "_all" },
                    {
                        'className': 'select-checkbox',
                        'targets': 0,
                        'checkboxes': {
                            'selectRow': true
                        }
                    }
                ],

                'select': {
                    'style': 'multi',
                    "selector": 'td:first-child'
                },
                'order': [[1, 'asc']]
            });

            $('#frm-example').on('submit', function (e) {
                var form = this;
                var rows_selected = table.column(0).checkboxes.selected();
                // Iterate over all selected checkboxes
                $.each(rows_selected, function (index, rowId) {
                    // Create a hidden element
                    $(form).append(
                        $('<input>')
                            .attr('type', 'hidden')
                            .attr('name', 'id[]')
                            .val(rowId)
                    );
                });
          
                // Output form data to a console it works
                //$('#example-console-rows').text(rows_selected.join(","));

                   // trying to send array to controller,  dsnt works      
                   $.ajax({
                    url: "@Url.Content("~/RADICACIONES/CerrarMasivo")",
                    type: 'post',
                    data: table.rows({ selected: true }).data().toArray(),
                    dataType: 'json',
                    success: function (returnedData) {
                        console.log(returnedData);
                    }
                });

            });


        });

**I need to send data to the controller, but is always with no data

//controller
public ActionResult CerrarMasivo(string[] data)
{
actions...
}

**: 

Edited by Colin - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    I suspect you'll need to wrap the data in JSON.stringify(), as you can't post objects.

    Colin

  • lbelalcazarlbelalcazar Posts: 2Questions: 1Answers: 0

    Thx Colin for your help, some things were solved with JSON.stringify, I couldn't fix the problem by passing all the information from the row, but I managed to create an array where I stored the primary key and pass it to the controller, It was not the most efficient solution, but it achieves the objective.

            // Handle form submission event
            $('#frm-example').on('submit', function (e) {
                var form = this;
    
                var rows_selected = table.column(0).checkboxes.selected();
                //var rows_selected = table.column(0)({ selected: true }).data();
                // Iterate over all selected checkboxes
                var ids = [];
                $.each(rows_selected, function (index, rowId) {
                    // Create a hidden element
                    ids.push(rowId);
                    $(form).append(
                        $('<input>')
                            .attr('type', 'hidden')
                            .attr('name', 'id[]')
                            .val(rowId)
                    );
    
                });
    
                $.ajax({
                    url: "@Url.Content("~/RADICACIONES/CerrarMasivo")",
                    type: 'POST',
                    data: JSON.stringify(ids),
                    contentType: 'application/json',
                    dataType: 'html',
                    success: function (data, status, xhr) {
                        alert('Success!');
                    },
                    error: function (xhr, status, error) {
                        alert('Update Error occurred - ' + error);
                    }
                });
                e.preventDefault();
            });
    

    **in the controller:

         public ActionResult CerrarMasivo(string[] idpqr)
         {
     code....
             return View();
         }
    
This discussion has been closed.