0

Web サービスにデータを入力するクラスがあります。これは JqGridData と呼ばれ、Oleg による別のスレッドへの返信としてインスピレーションを得て作成されました。次のようになります。

    public class JqGridData
{
    /// <summary>
    /// The number of pages which should be displayed in the paging controls at the bottom of the grid.
    /// </summary>
    public int Total { get; set; }
    /// <summary>
    /// The current page number which should be highlighted in the paging controls at the bottom of the grid.
    /// </summary>
    public int Page { get; set; }
    /// <summary>
    /// The total number of records in the entire data set, not just the portion returned in Rows.
    /// </summary>
    public int Records { get; set; }
    /// <summary>
    /// The data that will actually be displayed in the grid.
    /// </summary>
    public IEnumerable Rows { get; set; }
    /// <summary>
    /// Arbitrary data to be returned to the grid along with the row data. Leave null if not using. Must be serializable to JSON!
    /// </summary>
    public object UserData { get; set; }
}

私の Web サービスでは、次の方法でデータを返します。

            List<Order> orders = OrderBLL.LoadByCustomerIdPaging(customer.CustomerId, page, rows, sidx, sord, out  totalCount);

        object o = null;

        JqGridData result = new JqGridData()
        {
            Page = page,
            Total = (int)Math.Ceiling((decimal)totalCount / rows),
            Records = totalCount,
            Rows = (from order in orders
                    select new 
                    {
                        Id = order.OrderId,
                        OrderId = order.OrderId//,
                        //OrderStatusName = order.OrderStatusName,
                        //OrderDate = order.ActionDate.Value.Date.ToShortDateString(),
                        //OrderTypeName = order.OrderTypeName,
                        //OrderPhoneNo = order.PhoneNo,
                        //OrderProductName = order.Product.ProductName,
                        //OrderTempNumber = order.TempNumber,
                        //OrderLines = order.FormattedOrderlines
                    }).ToArray(),
            UserData = o

        };

        //return result;
        //return result.ConvertToJson();
        JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
        return jsonSerializer.Serialize(result);

私のjqGridは次のように定義されています:

        jQuery("#<%=orderList.ClientID %>").jqGrid({
        datatype: "json",
        url: "/Webservices/OrderService.svc/LoadOrderViewPaging",
        contentType: 'application/json; charset=utf-8',
        mtype: 'POST',
        jsonReader: {
            root: "d.Rows",
            page: "d.Page",
            total: "d.Total",
            records: "d.Records",
            repeatitems: false
        },
        height: 'auto',
        rowNum: 30,
        rowList: [10, 20, 30],
        shrinkToFit: true,
        colNames: [
                    '<%=GetGlobalResourceObject("CustomerOrders","OrderId") %>',
        //                        '<%=GetGlobalResourceObject("CustomerOrders","OrderStatus") %>',
        //                        '<%=GetGlobalResourceObject("CustomerOrders","OrderDate") %>',
        //                        '<%=GetGlobalResourceObject("CustomerOrders","OrderType") %>',
        //                        '<%=GetGlobalResourceObject("CustomerOrders","OrderPhoneNo") %>',
        //                        '<%=GetGlobalResourceObject("CustomerOrders","OrderTempNumber") %>',
        //                        '<%=GetGlobalResourceObject("CustomerOrders","OrderProduct") %>',
        //                        '<%=GetGlobalResourceObject("CustomerOrders","Products") %>'
                    ],
        colModel: [
        { name: 'OrderId', index: 'OrderId', width: 60, sorttype: "int" },
        //            { name: 'OrderStatusName', index: 'OrderStatusName', width: 80 },
        //            { name: 'ActionDate', index: 'ActionDate', formatter: 'date', formatoptions: { srcformat: 'ISO8601Long', newformat: 'Y-m-d H:i:s' }, width: 80 },
        //            { name: 'OrderTypeName', index: 'OrderTypeName', width: 80 },
        //            { name: 'PhoneNo', index: 'PhoneNo', width: 80 },
        //            { name: 'TempNumber', index: 'TempNumber', width: 80 },
        //            { name: 'Product.ProductName', index: 'OrderProductName', width: 125 },
        //            { name: 'FormattedOrderlines', index: 'FormattedOrderlines', width: 125 },
            ],
        pager: "#<%=orderListPager.ClientID %>",
        viewrecords: true,
        sortname: 'OrderId',
        sortorder: 'desc',
        viewrecords: true,
        sortname: 'Id',
        grouping: true,
        groupingView: {
            groupField: ['OrderId'],
            groupOrder: ['desc']
            //                groupCollapse: true,
        },
        loadError: function (jqXHR, textStatus, errorThrown) {
            alert('HTTP status code: ' + jqXHR.status + '\n' +
          'textStatus: ' + textStatus + '\n' +
          'errorThrown: ' + errorThrown);
            alert('HTTP message body (jqXHR.responseText): ' + '\n' + jqXHR.responseText);
        },
        beforeProcessing: function (data, status, xhr) {
            debugger;
        },
        caption: '<%=GetGlobalResourceObject("CustomerOrders","Customer orders") %>',
        onSelectRow: function (id) {
            if (id) {
                var ret = jQuery("#<%=orderList.ClientID %>").jqGrid('getRowData', id);
                ShowOrderStatus(ret.OrderId);
            }
        }

loadError はエラーをスローしません。beforeProcessing イベントをキャッチすると、以下に挿入した有効な json データを受け取っていることがわかります。

{"Total":73,"Page":1,"Records":2179,"Rows":[{"Id":30903,"OrderId":30903},{"Id":30852,"OrderId":30852},{"Id":30692,"OrderId":30692},{"Id":30687,"OrderId":30687},{"Id":30686,"OrderId":30686},{"Id":30684,"OrderId":30684},{"Id":30683,"OrderId":30683},{"Id":30682,"OrderId":30682},{"Id":30681,"OrderId":30681},{"Id":30680,"OrderId":30680},{"Id":30678,"OrderId":30678},{"Id":30677,"OrderId":30677},{"Id":30676,"OrderId":30676},{"Id":30675,"OrderId":30675},{"Id":30674,"OrderId":30674},{"Id":30673,"OrderId":30673},{"Id":30668,"OrderId":30668},{"Id":30667,"OrderId":30667},{"Id":30665,"OrderId":30665},{"Id":30664,"OrderId":30664},{"Id":30663,"OrderId":30663},{"Id":30647,"OrderId":30647},{"Id":30635,"OrderId":30635},{"Id":30411,"OrderId":30411},{"Id":30331,"OrderId":30331},{"Id":30330,"OrderId":30330},{"Id":30287,"OrderId":30287},{"Id":30241,"OrderId":30241},{"Id":30240,"OrderId":30240},{"Id":30239,"OrderId":30239}],"UserData":null}

ただし、jqGrid には行が表示されません。誰かがいくつかのアイデアを持っているなら、彼らは大歓迎です。

PS。JqG​​ridData の Rows フィールドを JqGridRecord のような定義されたクラスに変更し、これらすべてのリストを返すと、すべてが機能します。しかし、私が返したいすべての特定の戻りデータ構成に対してクラスを定義することを強制されたくありません。

更新しました

オブジェクトを返す限り、問題なく動作します。でもそれは防ぎたい。

JqGridData result = new JqGridData()
        {
            page = page,
            total = (int)Math.Ceiling((decimal)totalCount / rows),
            records = totalCount,
            rows = (from order in orders
                    select new jqGridRecord
                    {
                        OrderId = order.OrderId//,
                        //OrderId = order.OrderId//,
                        //OrderStatusName = order.OrderStatusName,
                        //OrderDate = order.ActionDate.Value.Date.ToShortDateString(),
                        //OrderTypeName = order.OrderTypeName,
                        //OrderPhoneNo = order.PhoneNo,
                        //OrderProductName = order.Product.ProductName,
                        //OrderTempNumber = order.TempNumber,
                        //OrderLines = order.FormattedOrderlines
                    }).ToList()//,
            //UserData = o

        };

        return result;
4

1 に答える 1

0

次のような方法でデータを jqGrid に返すことはできませんか。

...
var jsonData = new
            {
                total = (totalRecords + rows - 1) / rows,
                page = page,
                records = totalRecords,
                rows = (
                    from temp in pagedQuery.ToList()                    
                    select new
                    {
                        cell = new string[] {       
                             temp.item1,
                             temp.item2,
                             ...
                        }
                    }).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
于 2013-09-27T16:51:54.467 に答える