用数据集填充WebGrid

时间:2018-08-29 18:28:25

标签: javascript c# model-view-controller

我在将数据集返回到WebGrid时遇到问题,以前我是用到实体的连接来填充它的,并且我没有任何问题,事实是,出于安全考虑,我不能使用已经超过一个的连接”默认”

之前:

using (SiniestrosEntities dc = new SiniestrosEntities())
{
    var Condicion = dc.CAT_Doctos_Proveedores.OrderBy(a => 
a.id_Proveedor).ToList();

    return Json(new
    {
        data = Condicion
    }, JsonRequestBehavior.AllowGet);
}

将数据返回到View,我没问题。

现在我在DATSET中有我的数据库查询,我想用Json返回它到我的视图中……但是我得到以下错误:

  

DataTables警告:表id = myDatatable-请求的未知参数'nFacturaProveedoresID'用于第0行第0列。有关此错误的更多信息,请参见http://datatables.net/tn/4

我的代码JS

var oTable = $('#myDatatable').DataTable({
    "ajax": {
        "url": '/Facturas/GetEmployees',
        "type": "get",
        "datatype": "json"
    },
    "columns": [
        { "data": "nFacturaProveedoresID", "autoWidth": true },
        { "data": "nSiniestroid", "autoWidth": true },
        { "data": "cSerie", "autoWidth": true },
        { "data": "cFolio", "autoWidth": true },
        { "data": "dtFechaFactura", "autoWidth": true },
        { "data": "cConcepto", "autoWidth": true },
        { "data": "mTotal", "autoWidth": true },
        { "data": "dtFechaRegistro", "autoWidth": true },
        { "data": "nFacturaProveedoresID", "width": "20px", "render": function (data) {
            return '<a class="popup" href="/Facturas/Save/' + data + '">Editar</a>';
            }
        },
        {
            "data": "EmployeeID", "width": "50px", "render": function (data) 
            {
                return '<a class="popup" href="/Facturas/Delete' + data + '">Borrar</a>';
            }
        }
        ]
    })
    $('.tablecontainer').on('click', 'a.popup', function (e) {
        e.preventDefault();
        OpenPopup($(this).attr('href'));
    })
    function OpenPopup(pageUrl) {
        debugger;
        var $pageContent = $('<div/>');
        $pageContent.load(pageUrl, function () {
            $('#popupForm', $pageContent).removeData('validator');
            $('#popupForm', $pageContent).removeData('unobtrusiveValidation');
            $.validator.unobtrusive.parse('form');

        });

        $dialog = $('<div class="popupWindow" style="overflow:auto"></div>')
                          .html($pageContent)
                          .dialog({
                              draggable: true,
                              autoOpen: false,
                              resizable: true,
                              model: true,
                              title: 'Actualización de Datos',
                              height: 850,
                              width: 1650,
                              close: function () {
                                  $dialog.dialog('destroy').remove();
                              }
                          })
                debugger;
                $('.popupWindow').on('submit', '#popupForm', function (e) {
                    var url = $('#popupForm')[0].action;
                $.ajax({
                    type: "POST",
                    url: url,
                    data: $('#popupForm').serialize(),
                    success: function (data) {
                        if (data.status) {
                            $dialog.dialog('close');
                            oTable.ajax.reload();
                        }
                    }
                })

                e.preventDefault();
            })
            $dialog.dialog('open');
        }
    })

我的控制器

public ActionResult GetEmployees()
{
    try
    {
        int proveedor = Convert.ToInt32(Session["IdProveedor"]);

        DataSet ConsultaGrid = new DataSet();
        ConsultasProveedores ConsultaFacturas = new ConsultasProveedores();
        ConsultaGrid = ConsultaFacturas.ConsultaGen(Convert.ToString(proveedor));

        List<string> list = new List<string>();
        list.Add(ConsultaGrid.Tables[0].Rows[0]["nFacturaProveedoresID"].ToString());
        list.Add(ConsultaGrid.Tables[0].Rows[0]["nSiniestroid"].ToString());
        list.Add(ConsultaGrid.Tables[0].Rows[0]["cSerie"].ToString());
        list.Add(ConsultaGrid.Tables[0].Rows[0]["cFolio"].ToString());
        list.Add(ConsultaGrid.Tables[0].Rows[0]["dtFechaFactura"].ToString());
        list.Add(ConsultaGrid.Tables[0].Rows[0]["cConcepto"].ToString());
        list.Add(ConsultaGrid.Tables[0].Rows[0]["mTotal"].ToString());
        list.Add(ConsultaGrid.Tables[0].Rows[0]["dtFechaRegistro"].ToString());

        return Json(new
        {
            data = list
        }, JsonRequestBehavior.AllowGet);
        }
    }
    catch (Exception ex)
    {
        return View("Login");
    }            
}

1 个答案:

答案 0 :(得分:0)

您将返回字符串列表(每个列一个)。 Datatable将期望一个对象数组,每个对象都有定义的列:

"columns": [
    { "data": "nFacturaProveedoresID", "autoWidth": true },
    { "data": "nSiniestroid", "autoWidth": true },
    and so on

您的控制器应返回类似List<Proveedor>的内容,其中证明者为:

public class Proveedor {
    public int nFacturaProveedoresID {get; set; }
    public int nSiniestroid {get; set;}
    etc
}

您的控制器应类似于:

public ActionResult GetEmployees()
{
    try
    {
        int proveedor = Convert.ToInt32(Session["IdProveedor"]);

        DataSet ConsultaGrid = new DataSet();
        ConsultasProveedores ConsultaFacturas = new ConsultasProveedores();
        ConsultaGrid = ConsultaFacturas.ConsultaGen(Convert.ToString(proveedor));

        List<Proveedor> list = new List<Proveedor>();
        list.Add(new Proveedor {
           nFacturaProveedoresID = (int)ConsultaGrid.Tables[0].Rows[0]["nFacturaProveedoresID"],
           nSiniestroid = (int)ConsultaGrid.Tables[0].Rows[0]["nSiniestroid"],
           //rest of fields
        });


        return Json(new
        {
            data = list
        }, JsonRequestBehavior.AllowGet);
        }
    }
    catch (Exception ex)
    {
        return View("Login");
    }            
}