Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Jquery数据表从ajax绑定数据_C#_Jquery_Ajax_Asp.net Mvc_Datatable - Fatal编程技术网

C# Jquery数据表从ajax绑定数据

C# Jquery数据表从ajax绑定数据,c#,jquery,ajax,asp.net-mvc,datatable,C#,Jquery,Ajax,Asp.net Mvc,Datatable,我对如何从控制器绑定数据有疑问。 这就是想法: 我有数据表中的数据列表。我需要从datatable中删除1个数据。我正在使用ajax post在controller中调用actionresult并发送模型。我的控制器将删除数据,并返回模型以供查看 <script> var Display; $(document).ready(function () { $('#ProductTable').DataTable(); Display

我对如何从控制器绑定数据有疑问。 这就是想法: 我有数据表中的数据列表。我需要从datatable中删除1个数据。我正在使用ajax post在controller中调用actionresult并发送模型。我的控制器将删除数据,并返回模型以供查看

<script>
    var Display;

    $(document).ready(function () {

        $('#ProductTable').DataTable();

        Display = function () {
            var URL = '@Url.Action("GetProductsData", "Product")';

            oTable = $('#ProductTable').DataTable({
                dom: 'Bfrtip',
                "bPaginate": false,
                buttons: [
                    'excel', 'pdf', 'print'
                ],
                "processing": false,
                "serverSide": false,
                "bSort": false,
                "searching": true,
                "sAjaxSource": URL,
                "pageLength": 10,
                "bDestroy": true,
                "bLengthChange": true,
                "scrollX": true,
                "scrollY": ($(window).height() - 200),
                "pagingType": "full_numbers",
                "sEmptyTable": "Loading data from server",
                "fnServerData": function (sSource, aoData, fnCallback) {

                    $.ajax({
                        "dataType": 'json',
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": fnCallback
                    });
                },
                "columns": [
                                  {

                                      "sWidth": "5%",
                                      "bSortable": true,
                                      "sClass": "TextCenter ID",
                                      "visible": false,
                                      "render": function (data, type, row, meta) {
                                          return (row[0])
                                      }
                                  },
                                  {

                                      "sWidth": "5%",
                                      "sClass": "rightalign ",
                                      "render": function (data, type, row, meta) {
                                          return (row[1])
                                      }
                                  },
                                   {

                                       "sWidth": "10%",
                                       "sClass": "rightalign TA_C",
                                       "render": function (data, type, row, meta) {
                                           return (row[2])
                                       }
                                   },

                                  {
                                      "swidth": "5%",
                                      "sclass": "TextCenter Action",
                                      "render": function (data, type, row, meta) {
                                          return '<button class="btn btn-primary fa fa-check-square"  title="Edit" onclick="editdata(' + row[0] + ',\'' + row[1] + '\',' + row[2] + ')"></button>' +
                                      '<button class="btn btn-danger glyphicon glyphicon-trash" title="Delete"  onclick="deletedata(' + row[0] + ')" style="margin-left: 10px;"></button>';

                                          }
                                  }


                ], "fnInitComplete": function (oSetting, json) {

                }
            });
        }



        Display();

        $("#btninsert").click(function () {


            var fdata = new FormData();

            fdata.append("id","0");
            fdata.append("pname",$("#txtpname").val());
            fdata.append("pprice", $("#txtpprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Inserted!", "", "success", {
                            button: "Close",
                        });

                        clear();
                        Display();
                    }
                    else {

                        swal("Product Not Inserted!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });

        });


        $("#btnupdate").click(function () {
            var fdata = new FormData();

            fdata.append("id", $("#hdnID").val());
            fdata.append("pname", $("#txtuppname").val());
            fdata.append("pprice", $("#txtuppprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Updated!", "", "success", {
                            button: "Close",
                        });
                        clear();
                        Display();
                        $('#mmd').modal('hide');

                    }
                    else {
                        swal("Product Not Updated!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });
        });

        function clear() {
            $("#txtpname").val("");
            $("#txtpprice").val("");
        }
    });

    function deletedata(ID) {
        bootbox.confirm({
            title: "Please Confirm",
            message: "Are you sure to delete this record.",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                if (result == true) {
                    var data = { "ID": ID };
                    $.ajax({
                        url: '@Url.Action("deleteRecord", "Product")',
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(data),
                        dataType: "json",
                        success: function (response) {

                            if (response == 1) {
                                swal("Successfully Product Deleted!", "", "success", {
                                    button: "Close",
                                });

                                    Display();


                            }
                            else {
                                swal("Product Not Deleted!", "", "error", {
                                    button: "Close",
                                });

                            }
                        }
                    });
                }
            }
        });
    }

    function editdata(pid,pname,price)
    {

        $("#hdnID").val(pid);
        $("#txtuppprice").val(price);
        $("#txtuppname").val(pname);
        $('#mmd').modal();          
    }
</script>

<style>
    .dataTables_scrollBody{
            position: relative;
overflow: auto;
margin-top: -5%;
width: 100%;
height: 502px;
    }
</style>
以下是我的viewmodels:

public class SampleHeaderViewModels
{
    [Display(Name = "ID")]
    public int intID { get; set; }

    [Display(Name = "Field 1")]
    public string txtField1 { get; set; }

    [Display(Name = "Field 2")]
    public string txtField2 { get; set; }
}

public class SampleDetailViewModels
{
    [Display(Name = "ID")]
    public int intID { get; set; }

    [Display(Name = "Line")]
    public int intLine { get; set; }

    [Display(Name = "Detail 1")]
    public string txtDetail1 { get; set; }

    [Display(Name = "Detail 2")]
    public string txtDetail2 { get; set; }
}

public class SampleViewModels
{
    public SampleHeaderViewModels Header { get; set; }
    public List<SampleDetailViewModels> Detail { get; set; }

    public SampleViewModels()
    {
        Header = new SampleHeaderViewModels();
        Detail = new List<SampleDetailViewModels>();
    }
}
<script>
    var Display;

    $(document).ready(function () {

        $('#ProductTable').DataTable();

        Display = function () {
            var URL = '@Url.Action("GetProductsData", "Product")';

            oTable = $('#ProductTable').DataTable({
                dom: 'Bfrtip',
                "bPaginate": false,
                buttons: [
                    'excel', 'pdf', 'print'
                ],
                "processing": false,
                "serverSide": false,
                "bSort": false,
                "searching": true,
                "sAjaxSource": URL,
                "pageLength": 10,
                "bDestroy": true,
                "bLengthChange": true,
                "scrollX": true,
                "scrollY": ($(window).height() - 200),
                "pagingType": "full_numbers",
                "sEmptyTable": "Loading data from server",
                "fnServerData": function (sSource, aoData, fnCallback) {

                    $.ajax({
                        "dataType": 'json',
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": fnCallback
                    });
                },
                "columns": [
                                  {

                                      "sWidth": "5%",
                                      "bSortable": true,
                                      "sClass": "TextCenter ID",
                                      "visible": false,
                                      "render": function (data, type, row, meta) {
                                          return (row[0])
                                      }
                                  },
                                  {

                                      "sWidth": "5%",
                                      "sClass": "rightalign ",
                                      "render": function (data, type, row, meta) {
                                          return (row[1])
                                      }
                                  },
                                   {

                                       "sWidth": "10%",
                                       "sClass": "rightalign TA_C",
                                       "render": function (data, type, row, meta) {
                                           return (row[2])
                                       }
                                   },

                                  {
                                      "swidth": "5%",
                                      "sclass": "TextCenter Action",
                                      "render": function (data, type, row, meta) {
                                          return '<button class="btn btn-primary fa fa-check-square"  title="Edit" onclick="editdata(' + row[0] + ',\'' + row[1] + '\',' + row[2] + ')"></button>' +
                                      '<button class="btn btn-danger glyphicon glyphicon-trash" title="Delete"  onclick="deletedata(' + row[0] + ')" style="margin-left: 10px;"></button>';

                                          }
                                  }


                ], "fnInitComplete": function (oSetting, json) {

                }
            });
        }



        Display();

        $("#btninsert").click(function () {


            var fdata = new FormData();

            fdata.append("id","0");
            fdata.append("pname",$("#txtpname").val());
            fdata.append("pprice", $("#txtpprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Inserted!", "", "success", {
                            button: "Close",
                        });

                        clear();
                        Display();
                    }
                    else {

                        swal("Product Not Inserted!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });

        });


        $("#btnupdate").click(function () {
            var fdata = new FormData();

            fdata.append("id", $("#hdnID").val());
            fdata.append("pname", $("#txtuppname").val());
            fdata.append("pprice", $("#txtuppprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Updated!", "", "success", {
                            button: "Close",
                        });
                        clear();
                        Display();
                        $('#mmd').modal('hide');

                    }
                    else {
                        swal("Product Not Updated!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });
        });

        function clear() {
            $("#txtpname").val("");
            $("#txtpprice").val("");
        }
    });

    function deletedata(ID) {
        bootbox.confirm({
            title: "Please Confirm",
            message: "Are you sure to delete this record.",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                if (result == true) {
                    var data = { "ID": ID };
                    $.ajax({
                        url: '@Url.Action("deleteRecord", "Product")',
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(data),
                        dataType: "json",
                        success: function (response) {

                            if (response == 1) {
                                swal("Successfully Product Deleted!", "", "success", {
                                    button: "Close",
                                });

                                    Display();


                            }
                            else {
                                swal("Product Not Deleted!", "", "error", {
                                    button: "Close",
                                });

                            }
                        }
                    });
                }
            }
        });
    }

    function editdata(pid,pname,price)
    {

        $("#hdnID").val(pid);
        $("#txtuppprice").val(price);
        $("#txtuppname").val(pname);
        $('#mmd').modal();          
    }
</script>

<style>
    .dataTables_scrollBody{
            position: relative;
overflow: auto;
margin-top: -5%;
width: 100%;
height: 502px;
    }
</style>

如果成功从数据库中删除记录,则操作方法返回true。否则返回false:

 public ActionResult DeleteDetail(SampleHeaderViewModels objHeader, 
  List<SampleDetailViewModels> objDetail, int intLine)
  {
     ...
  ...
  db.SaveChanges();
  return Json(new { Success = true; });
  }
<script>
    var Display;

    $(document).ready(function () {

        $('#ProductTable').DataTable();

        Display = function () {
            var URL = '@Url.Action("GetProductsData", "Product")';

            oTable = $('#ProductTable').DataTable({
                dom: 'Bfrtip',
                "bPaginate": false,
                buttons: [
                    'excel', 'pdf', 'print'
                ],
                "processing": false,
                "serverSide": false,
                "bSort": false,
                "searching": true,
                "sAjaxSource": URL,
                "pageLength": 10,
                "bDestroy": true,
                "bLengthChange": true,
                "scrollX": true,
                "scrollY": ($(window).height() - 200),
                "pagingType": "full_numbers",
                "sEmptyTable": "Loading data from server",
                "fnServerData": function (sSource, aoData, fnCallback) {

                    $.ajax({
                        "dataType": 'json',
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": fnCallback
                    });
                },
                "columns": [
                                  {

                                      "sWidth": "5%",
                                      "bSortable": true,
                                      "sClass": "TextCenter ID",
                                      "visible": false,
                                      "render": function (data, type, row, meta) {
                                          return (row[0])
                                      }
                                  },
                                  {

                                      "sWidth": "5%",
                                      "sClass": "rightalign ",
                                      "render": function (data, type, row, meta) {
                                          return (row[1])
                                      }
                                  },
                                   {

                                       "sWidth": "10%",
                                       "sClass": "rightalign TA_C",
                                       "render": function (data, type, row, meta) {
                                           return (row[2])
                                       }
                                   },

                                  {
                                      "swidth": "5%",
                                      "sclass": "TextCenter Action",
                                      "render": function (data, type, row, meta) {
                                          return '<button class="btn btn-primary fa fa-check-square"  title="Edit" onclick="editdata(' + row[0] + ',\'' + row[1] + '\',' + row[2] + ')"></button>' +
                                      '<button class="btn btn-danger glyphicon glyphicon-trash" title="Delete"  onclick="deletedata(' + row[0] + ')" style="margin-left: 10px;"></button>';

                                          }
                                  }


                ], "fnInitComplete": function (oSetting, json) {

                }
            });
        }



        Display();

        $("#btninsert").click(function () {


            var fdata = new FormData();

            fdata.append("id","0");
            fdata.append("pname",$("#txtpname").val());
            fdata.append("pprice", $("#txtpprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Inserted!", "", "success", {
                            button: "Close",
                        });

                        clear();
                        Display();
                    }
                    else {

                        swal("Product Not Inserted!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });

        });


        $("#btnupdate").click(function () {
            var fdata = new FormData();

            fdata.append("id", $("#hdnID").val());
            fdata.append("pname", $("#txtuppname").val());
            fdata.append("pprice", $("#txtuppprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Updated!", "", "success", {
                            button: "Close",
                        });
                        clear();
                        Display();
                        $('#mmd').modal('hide');

                    }
                    else {
                        swal("Product Not Updated!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });
        });

        function clear() {
            $("#txtpname").val("");
            $("#txtpprice").val("");
        }
    });

    function deletedata(ID) {
        bootbox.confirm({
            title: "Please Confirm",
            message: "Are you sure to delete this record.",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                if (result == true) {
                    var data = { "ID": ID };
                    $.ajax({
                        url: '@Url.Action("deleteRecord", "Product")',
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(data),
                        dataType: "json",
                        success: function (response) {

                            if (response == 1) {
                                swal("Successfully Product Deleted!", "", "success", {
                                    button: "Close",
                                });

                                    Display();


                            }
                            else {
                                swal("Product Not Deleted!", "", "error", {
                                    button: "Close",
                                });

                            }
                        }
                    });
                }
            }
        });
    }

    function editdata(pid,pname,price)
    {

        $("#hdnID").val(pid);
        $("#txtuppprice").val(price);
        $("#txtuppname").val(pname);
        $('#mmd').modal();          
    }
</script>

<style>
    .dataTables_scrollBody{
            position: relative;
overflow: auto;
margin-top: -5%;
width: 100%;
height: 502px;
    }
</style>
Jquery:

<script>
    var Display;

    $(document).ready(function () {

        $('#ProductTable').DataTable();

        Display = function () {
            var URL = '@Url.Action("GetProductsData", "Product")';

            oTable = $('#ProductTable').DataTable({
                dom: 'Bfrtip',
                "bPaginate": false,
                buttons: [
                    'excel', 'pdf', 'print'
                ],
                "processing": false,
                "serverSide": false,
                "bSort": false,
                "searching": true,
                "sAjaxSource": URL,
                "pageLength": 10,
                "bDestroy": true,
                "bLengthChange": true,
                "scrollX": true,
                "scrollY": ($(window).height() - 200),
                "pagingType": "full_numbers",
                "sEmptyTable": "Loading data from server",
                "fnServerData": function (sSource, aoData, fnCallback) {

                    $.ajax({
                        "dataType": 'json',
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": fnCallback
                    });
                },
                "columns": [
                                  {

                                      "sWidth": "5%",
                                      "bSortable": true,
                                      "sClass": "TextCenter ID",
                                      "visible": false,
                                      "render": function (data, type, row, meta) {
                                          return (row[0])
                                      }
                                  },
                                  {

                                      "sWidth": "5%",
                                      "sClass": "rightalign ",
                                      "render": function (data, type, row, meta) {
                                          return (row[1])
                                      }
                                  },
                                   {

                                       "sWidth": "10%",
                                       "sClass": "rightalign TA_C",
                                       "render": function (data, type, row, meta) {
                                           return (row[2])
                                       }
                                   },

                                  {
                                      "swidth": "5%",
                                      "sclass": "TextCenter Action",
                                      "render": function (data, type, row, meta) {
                                          return '<button class="btn btn-primary fa fa-check-square"  title="Edit" onclick="editdata(' + row[0] + ',\'' + row[1] + '\',' + row[2] + ')"></button>' +
                                      '<button class="btn btn-danger glyphicon glyphicon-trash" title="Delete"  onclick="deletedata(' + row[0] + ')" style="margin-left: 10px;"></button>';

                                          }
                                  }


                ], "fnInitComplete": function (oSetting, json) {

                }
            });
        }



        Display();

        $("#btninsert").click(function () {


            var fdata = new FormData();

            fdata.append("id","0");
            fdata.append("pname",$("#txtpname").val());
            fdata.append("pprice", $("#txtpprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Inserted!", "", "success", {
                            button: "Close",
                        });

                        clear();
                        Display();
                    }
                    else {

                        swal("Product Not Inserted!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });

        });


        $("#btnupdate").click(function () {
            var fdata = new FormData();

            fdata.append("id", $("#hdnID").val());
            fdata.append("pname", $("#txtuppname").val());
            fdata.append("pprice", $("#txtuppprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Updated!", "", "success", {
                            button: "Close",
                        });
                        clear();
                        Display();
                        $('#mmd').modal('hide');

                    }
                    else {
                        swal("Product Not Updated!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });
        });

        function clear() {
            $("#txtpname").val("");
            $("#txtpprice").val("");
        }
    });

    function deletedata(ID) {
        bootbox.confirm({
            title: "Please Confirm",
            message: "Are you sure to delete this record.",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                if (result == true) {
                    var data = { "ID": ID };
                    $.ajax({
                        url: '@Url.Action("deleteRecord", "Product")',
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(data),
                        dataType: "json",
                        success: function (response) {

                            if (response == 1) {
                                swal("Successfully Product Deleted!", "", "success", {
                                    button: "Close",
                                });

                                    Display();


                            }
                            else {
                                swal("Product Not Deleted!", "", "error", {
                                    button: "Close",
                                });

                            }
                        }
                    });
                }
            }
        });
    }

    function editdata(pid,pname,price)
    {

        $("#hdnID").val(pid);
        $("#txtuppprice").val(price);
        $("#txtuppname").val(pname);
        $('#mmd').modal();          
    }
</script>

<style>
    .dataTables_scrollBody{
            position: relative;
overflow: auto;
margin-top: -5%;
width: 100%;
height: 502px;
    }
</style>

<script>
    var Display;

    $(document).ready(function () {

        $('#ProductTable').DataTable();

        Display = function () {
            var URL = '@Url.Action("GetProductsData", "Product")';

            oTable = $('#ProductTable').DataTable({
                dom: 'Bfrtip',
                "bPaginate": false,
                buttons: [
                    'excel', 'pdf', 'print'
                ],
                "processing": false,
                "serverSide": false,
                "bSort": false,
                "searching": true,
                "sAjaxSource": URL,
                "pageLength": 10,
                "bDestroy": true,
                "bLengthChange": true,
                "scrollX": true,
                "scrollY": ($(window).height() - 200),
                "pagingType": "full_numbers",
                "sEmptyTable": "Loading data from server",
                "fnServerData": function (sSource, aoData, fnCallback) {

                    $.ajax({
                        "dataType": 'json',
                        "type": "POST",
                        "url": sSource,
                        "data": aoData,
                        "success": fnCallback
                    });
                },
                "columns": [
                                  {

                                      "sWidth": "5%",
                                      "bSortable": true,
                                      "sClass": "TextCenter ID",
                                      "visible": false,
                                      "render": function (data, type, row, meta) {
                                          return (row[0])
                                      }
                                  },
                                  {

                                      "sWidth": "5%",
                                      "sClass": "rightalign ",
                                      "render": function (data, type, row, meta) {
                                          return (row[1])
                                      }
                                  },
                                   {

                                       "sWidth": "10%",
                                       "sClass": "rightalign TA_C",
                                       "render": function (data, type, row, meta) {
                                           return (row[2])
                                       }
                                   },

                                  {
                                      "swidth": "5%",
                                      "sclass": "TextCenter Action",
                                      "render": function (data, type, row, meta) {
                                          return '<button class="btn btn-primary fa fa-check-square"  title="Edit" onclick="editdata(' + row[0] + ',\'' + row[1] + '\',' + row[2] + ')"></button>' +
                                      '<button class="btn btn-danger glyphicon glyphicon-trash" title="Delete"  onclick="deletedata(' + row[0] + ')" style="margin-left: 10px;"></button>';

                                          }
                                  }


                ], "fnInitComplete": function (oSetting, json) {

                }
            });
        }



        Display();

        $("#btninsert").click(function () {


            var fdata = new FormData();

            fdata.append("id","0");
            fdata.append("pname",$("#txtpname").val());
            fdata.append("pprice", $("#txtpprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Inserted!", "", "success", {
                            button: "Close",
                        });

                        clear();
                        Display();
                    }
                    else {

                        swal("Product Not Inserted!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });

        });


        $("#btnupdate").click(function () {
            var fdata = new FormData();

            fdata.append("id", $("#hdnID").val());
            fdata.append("pname", $("#txtuppname").val());
            fdata.append("pprice", $("#txtuppprice").val());


            $.ajax({
                url: '@Url.Action("InupProduct", "Product")',
                type: "POST",
                contentType: false, // Not to set any content header
                processData: false, // Not to process data
                data: fdata,
                success: function (result) {
                    if (result == 1) {

                        swal("Successfully Product Updated!", "", "success", {
                            button: "Close",
                        });
                        clear();
                        Display();
                        $('#mmd').modal('hide');

                    }
                    else {
                        swal("Product Not Updated!", "", "error", {
                            button: "Close",
                        });
                    }

                },
                error: function (err) {
                    alert(err.statusText);
                }
            });
        });

        function clear() {
            $("#txtpname").val("");
            $("#txtpprice").val("");
        }
    });

    function deletedata(ID) {
        bootbox.confirm({
            title: "Please Confirm",
            message: "Are you sure to delete this record.",
            buttons: {
                cancel: {
                    label: '<i class="fa fa-times"></i> Cancel'
                },
                confirm: {
                    label: '<i class="fa fa-check"></i> Confirm'
                }
            },
            callback: function (result) {
                if (result == true) {
                    var data = { "ID": ID };
                    $.ajax({
                        url: '@Url.Action("deleteRecord", "Product")',
                        type: "POST",
                        contentType: "application/json; charset=utf-8",
                        data: JSON.stringify(data),
                        dataType: "json",
                        success: function (response) {

                            if (response == 1) {
                                swal("Successfully Product Deleted!", "", "success", {
                                    button: "Close",
                                });

                                    Display();


                            }
                            else {
                                swal("Product Not Deleted!", "", "error", {
                                    button: "Close",
                                });

                            }
                        }
                    });
                }
            }
        });
    }

    function editdata(pid,pname,price)
    {

        $("#hdnID").val(pid);
        $("#txtuppprice").val(price);
        $("#txtuppname").val(pname);
        $('#mmd').modal();          
    }
</script>

<style>
    .dataTables_scrollBody{
            position: relative;
overflow: auto;
margin-top: -5%;
width: 100%;
height: 502px;
    }
</style>

var显示;
$(文档).ready(函数(){
$('#ProductTable').DataTable();
显示=功能(){
var URL='@URL.Action(“GetProductsData”、“Product”);
oTable=$('#ProductTable')。数据表({
dom:'Bfrtip',
“bPaginate”:错误,
按钮:[
“excel”、“pdf”、“打印”
],
“处理”:假,
“服务器端”:false,
“bSort”:错误,
“搜索”:没错,
“sAjaxSource”:URL,
“页面长度”:10,
是的,
“bLengthChange”:正确,
“scrollX”:正确,
“滚动”:($(窗口).height()-200),
“pagingType”:“完整编号”,
“sEmptyTable”:“正在从服务器加载数据”,
“fnServerData”:函数(sSource、aoData、fnServerData回调){
$.ajax({
“数据类型”:“json”,
“类型”:“职位”,
“url”:sSource,
“数据”:aoData,
"成功":
});
},
“栏目”:[
{
“瑞士”:“5%”,
“可移植”:是的,
“sClass”:“文本中心ID”,
“可见”:假,
“呈现”:函数(数据、类型、行、元){
返回(第[0]行)
}
},
{
“瑞士”:“5%”,
“sClass”:“rightalign”,
“呈现”:函数(数据、类型、行、元){
返回(第[1]行)
}
},
{
“瑞士”:“10%”,
“sClass”:“rightalign TA_C”,
“呈现”:函数(数据、类型、行、元){
返回(第[2]行)
}
},
{
“瑞士”:“5%”,
“sclass”:“文本中心操作”,
“呈现”:函数(数据、类型、行、元){
返回“”+
'';
}
}
],“fnInitComplete”:函数(oSetting,json){
}
});
}
显示();
$(“#btn插入”)。单击(函数(){
var fdata=新表单数据();
fdata.append(“id”,“0”);
fdata.append(“pname”,$(“#txtpname”).val();
fdata.append(“pprice”,$(“#txtprice”).val();
$.ajax({
url:'@url.Action(“InupProduct”、“Product”),
类型:“POST”,
contentType:false,//不设置任何内容头
processData:false,//不处理数据
数据:fdata,
成功:功能(结果){
如果(结果==1){
swal(“成功插入产品!”、“成功”{
按钮:“关闭”,
});
清除();
显示();
}
否则{
swal(“产品未插入!”、“错误”{
按钮:“关闭”,
});
}
},
错误:函数(err){
警报(错误状态文本);
}
});
});
$(“#b更新”)。单击(函数(){
var fdata=新表单数据();
fdata.append(“id”,$(“#hdnID”).val();
fdata.append(“pname”,$(“#txtuppname”).val();
fdata.append(“pprice”,$(“txtpprice”).val();
$.ajax({
url:'@url.Action(“InupProduct”、“Product”),
类型:“POST”,
contentType:false,//不设置任何内容头
processData:false,//不处理数据
数据:fdata,
成功:功能(结果){
如果(结果==1){
swal(“成功更新产品!”、“成功”{
按钮:“关闭”,
});
清除();
显示();
$('#mmd').modal('hide');
}
否则{
swal(“产品未更新!”、“错误”{
按钮:“关闭”,
});
}
},
错误:函数(err){
警报(错误状态文本);
}
});
});