带有 SQL 绑定和 LargeJsonResult 的 jqxGrid 为空

jqxGrid with SQL Binding and LargeJsonResult is empty

本文关键字:jqxGrid 为空 LargeJsonResult SQL 绑定 带有      更新时间:2023-09-26

我在让 jqxGrid 与我的 SQL 数据绑定和 largejsonresult 一起工作时遇到问题。 这是我当前的代码。

控制器:

public LargeJsonResult GetCustomers()
    {
        var dbResult = db.CPTs.ToList();
        var customers = from customer in dbResult
                        select new
                        {
                            customer.CPT1,
                            customer.MOD,
                            customer.SDESC,
                            customer.FAGE,
                            customer.TAGE
                        };
        return new LargeJsonResult { Data = customers, JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet };
    }

视图:

<script type="text/javascript">
    $(document).ready(function () {
        // prepare the data
        var source = {
            datatype: "json",
            datafields: [{ name: 'CPT1' }, { name: 'MOD' },
            { name: 'SDESC' }, { name: 'FAGE' }, { name: 'TAGE' }, ],
            url: 'dbCPT/GetCustomers'
        };
        $("#jqxgrid").jqxGrid({
            source: source,
            theme: 'classic',
            columns: [{ text: 'Company Name', datafield: 'CompanyName',
                width: 250
            }, { text: 'CPT Code', datafield: 'CPT1', width: 150 },
{ text: 'Short Description', datafield: 'SDESC', width: 180 },
{ text: 'FAGE', datafield: 'From Age', width: 200 },
{ text: 'TAGE', datafield: 'To Age', width: 120}]
        });
    });
</script>
<h2>Index</h2>
<div id="jqxgrid"></div>

如果我直接运行dbCPT/GetCustomers,我会得到一个输出文件,其中包含我正在提取的所有数据,但是当转到dbCPT页面时,网格中不会出现任何内容。

如果您需要更多我的代码来提供帮助,请告诉我。

谢谢

我做了这项工作。在下面的代码中,我正在使用jqxDataAdapter,但我认为没有必要。

JavaScript

var source = {
    datatype: "json",
    datafields: [{ name: 'CompanyName' }, { name: 'ContactName' },
    { name: 'ContactTitle' }, { name: 'Address' }, { name: 'City'} ],
    url: 'Customers/GetCustomers'
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxgrid").jqxGrid({
    source: dataAdapter,
    theme: 'classic',
    columns: [{ text: 'Company Name', datafield: 'CompanyName', width: 250 },
                { text: 'Contact Name', datafield: 'ContactName', width: 150 },
                { text: 'Contact Title', datafield: 'ContactTitle', width: 180 },
                { text: 'Address', datafield: 'Address', width: 200 },
                { text: 'City', datafield: 'City', width: 120}]
});

C#

public LargeJsonResult GetCustomers2()
{
    var dbResult = db.Customers.ToList();
    var customers = from customer in dbResult
                    select new 
                    { 
                        customer.CompanyName, 
                        customer.ContactName, 
                        customer.ContactTitle, 
                        customer.Address, 
                        customer.City 
                    };
    return new LargeJsonResult { Data = customers, JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.AllowGet };
} 

确保您使用的是最新版本的 jqxGrid 和 LargeJsonResult 的正确实现。