如何将数据从jquery数据表发布到控制器? [英] How do I post data from jquery datatable to controller ?

查看:75
本文介绍了如何将数据从jquery数据表发布到控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Jquery数据表,其中填充了Ajax源数据。我需要在表格复选框中向控制器显示来自Ajax源的所有数据。由于这不是强类型视图,我不确定我必须将哪种类型的对象或数组传递给控制器​​方法。我尝试过的东西总是会返回一个空的结果。任何人都可以帮我如何将表数据发布到控制器?非常感谢提前。



我尝试过:



<

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 } };
    }

推荐答案

.ajax({
url:'/ ADM_MAS_Privilege / GetFormData',
数据:{YourValue:
.ajax({ url: '/ADM_MAS_Privilege/GetFormData', data: { YourValue:


('#productCategory')。val()},
方法:'post',
dataType:'json ',
成功:函数(数据){
var table =
('#productCategory').val() }, method: 'post', dataType: 'json', success: function (data) { var table =


('#jqueryTable')。dataTable({
paging:true,
sort:true,
搜索:true,
scrollY:200,
data:data,
bDestroy:true,

columnDefs :
[{
targets:2,
data:'IsTaxExempt',
render:function(data,type,full,meta){
返回数据? '< input type =复选框已停用已检查/>':'< input type =复选框已启用/>'
}
},

{
targets:3,
data:'IsTaxExempt',
render:function(data,type,full,meta){
返回数据? '< input type =复选框已停用已检查/>':'< input type =复选框已启用/>'
}
},

{
targets:4,
data:'IsTaxExempt',
render:function(data,type,full,meta){
返回数据? '< input type =checkbox已禁用已选中/>':'< input type =复选框已启用/>'
}

}],


列:[{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:查看,

orderable:false,

type:checkbox,
className:'select-checkbox'

}

]
});
}
});
};
我的保存功能:
('#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:


这篇关于如何将数据从jquery数据表发布到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆