Click here to Skip to main content
15,887,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Jquery datatable which is populated with Ajax sourced data. I need to show all the data from the Ajax source in the table checkbox checked to the controller. Since this is not strongly typed view I'm not sure what type of an object or array I have to pass to the controller method. The things that I have tried, always returns an empty result. Can anyone help me how to post table data to the controller? Thanks a lot in advance.

What I have tried:

<
My table:

<table id="jqueryTable" class="table table-striped table-bordered" cellspacing="0">
<thead>
    <tr>
        <th>
            ID
        </th>
        <th>
            PRIV_Name_Str
        </th>
        <th>
            Create
        </th>
        <th>
            Edit
        </th>
        <th>
            View
        </th>
    </tr>
</thead>
function LoadProduct(element) {
$.ajax({
    url: '/ADM_MAS_Privilege/GetFormData',
    data: { YourValue: $('#productCategory').val() },
    method: 'post',
    dataType: 'json',
    success: function (data) {
        var table =   $('#jqueryTable').dataTable({
            paging: true,
            sort: true,
            searching: true,
            scrollY: 200,
            data: data,
            bDestroy: true,

            "columnDefs":
            [{
                "targets": 2,
                "data": 'IsTaxExempt',
                "render": function (data, type, full, meta) {
                    return data ? '<input type="checkbox" disabled checked/>' : '<input type="checkbox" enabled />'
                }
            },

            {
                "targets": 3,
                "data": 'IsTaxExempt',
                "render": function (data, type, full, meta) {
                    return data ? '<input type="checkbox" disabled checked/>' : '<input type="checkbox" enabled />'
                }
            },

            {
                "targets": 4, 
                "data": 'IsTaxExempt',
                "render": function (data, type, full, meta) {
                    return data ? '<input type="checkbox" disabled checked/>' : '<input type="checkbox" enabled />'
                }

            }],


            columns: [{ "data": "ID", "ID": "ID", "autoWidth": true },
            {
                "data": "PRIV_Name_Str", "PRIV_Name_Str": "PRIV_Name_Str", "autoWidth": true
            },
            {
                "data": "Create", "Create": "Create", "autoWidth": true
            },
            { "data": "Edit", "Edit": "Edit", "autoWidth": true },
            {
                "data": "View",

                "orderable": false,

                "type": "checkbox",
                className: 'select-checkbox'

            }

            ]
        });
    }
});
};
My save function:

 $("form").on("submit", function () {
    var data = table.$("input, select").serialize();


$.ajax({
    type: "POST",
    url: '/ADM_MAS_Privilege/save',
    data: data,
    success: function (response) {
    }
});
This is my controller:

 public JsonResult save(Array[] data)
    {
        bool status = false;

        ADM_MAS_Privilege privladge = new ADM_MAS_Privilege();
        privladge = datatoobject(data);

        if (ModelState.IsValid)
        {

            db.ADM_MAS_Privilege.Add(privladge);
            db.SaveChanges();
            return new JsonResult { Data = new { status = status } };
        }


        return new JsonResult { Data = new { status = status } };
    }
Posted
Updated 13-May-18 20:02pm

1 solution

Your controller defines how it knows to receive data, either by a model, or by examining the posted data. You can post anything you like. Just make sure the other end knows how to read it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900