jqGrid,ASP.NET,JSON让我发疯。请帮忙

时间:2009-04-07 01:03:36

标签: asp.net jquery json jqgrid

我一直试图让它连续几天工作,这似乎是一件又一件事。最初,我最终将其跟踪到将JSON字符串中的ASP.NET日期传递回网格时出现问题。我现在正在使用JSON.NET,并且日期会以他们应该的方式回归我被困在我看起来似乎的位置。这是我的代码,返回json字符串,以及jqGrid代码:(对不起,它太长了,但我只是不想留下任何东西,以防万一这是让我搞砸的部分)!基本上,我一直坚持使用ajax调用的成功方法,我尝试添加JSONData ....现在我从Firebug得到一个错误,说: 无效的属性ID  ({总:1,页:1,记录:5,[{ “ROWID”:1, “... RNAME”: “BCC”, “SubmitterID”: “BCC4010”,“苏

任何帮助都会受到极大的赞赏!我想要使用这个插件这么多,但我绝对不会试图让它工作!


先谢谢,Briana: - )

WebMethod:
<WebMethod()> _
    <Script.Services.ScriptMethod(UseHttpGet:=False, ResponseFormat:=ResponseFormat.Json)> _
    Public Function GetTableData() As String
        Dim objController As New TradingPartnersController
        Dim gv_page As String = String.Empty
        Dim sidx As String = String.Empty
        Dim sord As String = String.Empty
        Dim start As String = String.Empty
        Dim limit As String = String.Empty
        Dim row As String = String.Empty
        Dim header As String = String.Empty
        Dim count As Integer = 0
        Dim total_pages As Integer = 0

        gv_page = HttpContext.Current.Request.Form("page")  '.Form("page")
        ' get the requested page 
        limit = HttpContext.Current.Request.Form("rows")
        ' get how many rows we want to have into the grid 
        sidx = HttpContext.Current.Request.Form("sidx")
        ' get index row - i.e. user click to sort 
        sord = HttpContext.Current.Request.Form("sord")
        ' get the direction 
        If String.IsNullOrEmpty(sidx) Then
            sidx = "PartnerID"

        End If

        If String.IsNullOrEmpty(sord) Then
            sord = "ASC"

        End If

        If String.IsNullOrEmpty(limit) Then
            limit = 10

        End If

        '' connect to the database 
        'GridView1.DataSource = objController.ListAll()
        'GridView1.DataBind()

        Dim objCollection As List(Of TradingPartnersInfo) = objController.ListAll()
        count = objCollection.Count

        If count > 0 Then
            total_pages = Math.Ceiling(count / Int32.Parse(limit))
        Else
            total_pages = 0
        End If

        If gv_page Is Nothing Then
            gv_page = 1
        End If
        If Not gv_page > total_pages Then
            gv_page = total_pages
        End If

        start = Math.Ceiling(Int32.Parse(limit) * (Int32.Parse(gv_page) - Int32.Parse(limit)))
        If start < 1 Then start = 1
        Dim objPageCollection As List(Of TradingPartnersInfo) = objController.ListTradingPartners(sidx, sord, Int32.Parse(start), Int32.Parse(limit))
        Dim json As New StringBuilder
        json.Append("{")
        json.Append("total:" & total_pages.ToString & ",")
        json.Append("page:" & gv_page.ToString & ",")
        json.Append("records:" & count.ToString & ",")
        json.Append(JsonConvert.SerializeObject(objPageCollection))
        json.Append("}")
        Return json.ToString

    End Function


Here is the jqGrid code:  


jQuery('table.scroll').jqGrid({  
    jsonReader: {  
        root: "rows", //arry containing actual data  
        page: "page", //current page  
        total: "total", //total pages for the query  
        records: "records", //total number of records  
        repeatitems: false,  
        id: "ID" //index of the column with the PK in it   
    },  
    datatype: function(postdata) {  
        jQuery.jmsajax({  
            type: "POST",  
            url: "EDI.asmx",  
            method: "GetTableData",  
            dataType: "msjson",  
            data: {},  
            complete: function(data) {  
                var mygrid = jQuery('table.scroll')[0];  
                var result = (eval("(" + data.responseText + ")"));  
                var myjsongrid = (jeval(result.d));  
                alert(myjsongrid.rows);  
                mygrid.addJSONData(jeval(myjsongrid.rows));  //This is ERROR LINE

                myjsongrid = null;  
                myjsongridParsed = null;  
                result = null;  
            },  
            success: function(data) {  
                alert('success: ' + data);  
            },  
            error: function(xhr) {  
                var res = xhr.responseText;  
                alert(res);  
            }  
        });  
    },  
    colNames: [
                    "ROWID",
                    "ID",
                    "PartnerID",
                        "Direction",
                        "InterchangeVersion",
                        "InterchangeSenderID",
                        "InterchangeReceiverID",
                        "ProductionMode",
                        "SubmitterName",
                        "SubmitterID",
                        "SubmitterPOC",
                        "CommQual",
                        "CommNumber",
                        "ReceiverName",
                        "ReceiverID",
                        "PartnerType",
                        "PartnerNotes",
                        "IncomingSP",
                        "OutgoingSP",
                        "ExchangeAck",
                        "isDeleted",
                        "DateTimeInserted",
                        "KeyID"
                    ],

    colModel: [
                { name: "ROWID",
                    width: 1,
                    hidden: true,
                    key: false,
                    sorttype: 'int',
                    editrules: { searchhidden: false,
                        hidedlg: true,
                        index: 'ROWID',
                        jsonmap: 'ROWID'
                    }
                },

                { name: "ID",
                    width: 1,
                    hidden: true,
                    key: true,
                    sorttype: 'int',
                    editrules: { searchhidden: true,
                        hidedlg: false,
                        index: 'ID',
                        jsonmap: 'ID'
                    }
                },

                { name: "PartnerID",
                    width: 50,
                    resizable: true,
                    sortable: true,
                    sorttype: 'text',
                    editable: true,
                    edittype: 'text',
                    editrules: { required: true,
                        index: 'PartnerID',
                        jsonmap: 'PartnerID',
                        label: 'PartnerID',
                        resizable: true,
                        search: true
                    }
                },
                    { name: "Direction",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'select',
                        editoptions: { value: "I:O" },
                        editrules: { required: true,
                            index: 'Direction',
                            jsonmap: 'Direction',
                            label: 'Direction',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "InterchangeVersion",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'select',
                        editoptions: { value: "004010X098A1:004010X097A1:004010X096A1:004010X098A:004010X097A:004010X096A" },
                        editrules: { required: true,
                            index: 'InterchangeVersion',
                            jsonmap: 'InterchangeVersion',
                            label: 'InterchangeVersion',
                            resizable: true, search: true
                        }
                    },

                    { name: "InterchangeSenderID",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: true,
                            index: 'InterchangeSenderID',
                            jsonmap: 'InterchangeSenderID',
                            label: 'InterchangeSenderID',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "InterchangeReceiverID",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: true,
                            index: 'InterchangeReceiverID',
                            jsonmap: 'InterchangeReceiverID',
                            label: 'InterchangeReceiverID',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "ProductionMode",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'select',
                        editoptions: { value: "T:P" },
                        editrules: { required: true,
                            index: 'ProductionMode',
                            jsonmap: 'ProductionMode',
                            label: 'ProductionMode',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "SubmitterName",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: true,
                            index: 'SubmitterName',
                            jsonmap: 'SubmitterName',
                            label: 'SubmitterName',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "SubmitterID",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: true,
                            index: 'SubmitterID',
                            jsonmap: 'SubmitterID',
                            label: 'SubmitterID',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "SubmitterPOC",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: true,
                            index: 'SubmitterPOC',
                            jsonmap: 'SubmitterPOC',
                            label: 'SubmitterPOC',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "CommQual",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: "select",
                        editoptions: { value: "ED:EM:FX:TE" },
                        editrules: { required: false,
                            index: 'CommQual',
                            jsonmap: 'CommQual',
                            label: 'CommQual',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "CommNumber",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: false,
                            index: 'CommNumber',
                            jsonmap: 'CommNumber',
                            label: 'CommNumber',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "ReceiverName",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: true,
                            index: 'ReceiverName',
                            jsonmap: 'ReceiverName',
                            label: 'ReceiverName',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "ReceiverID",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: true,
                            index: 'ReceiverID',
                            jsonmap: 'ReceiverID',
                            label: 'ReceiverID',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "PartnerType",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: "select",
                        editoptions: { value: "Clearinghouse:PPO:Both" },
                        editrules: { required: true,
                            index: 'PartnerType',
                            jsonmap: 'PartnerType',
                            label: 'PartnerType',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "PartnerNotes",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'textarea',
                        editrules: { required: false,
                            index: 'PartnerNotes',
                            jsonmap: 'PartnerNotes',
                            label: 'PartnerNotes',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "IncomingDataPrepSP",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: false,
                            index: 'IncomingDataPrepSP',
                            jsonmap: 'IncomingDataPrepSP',
                            label: 'IncomingDataPrepSP',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "OutgoingDataPrepSP",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: 'text',
                        editrules: { required: false,
                            index: 'OugoingDataPrepSP',
                            jsonmap: 'OugoingDataPrepSP',
                            label: 'OutgoingDataPrepSP',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "ExchangeAck",
                        width: 50,
                        resizable: true,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: "select",
                        editoptions: { value: "True:False" },
                        editrules: { required: true,
                            index: 'ExchangeAck',
                            jsonmap: 'ExchangeAck',
                            label: 'ExchangeAck',
                            resizable: true,
                            search: true
                        }
                    },

                    { name: "isDeleted",
                        width: 5,
                        resizable: false,
                        sortable: true,
                        sorttype: 'text',
                        editable: true,
                        edittype: "select",
                        editoptions: { value: "Yes:No" },
                        editrules: { required: true,
                            index: 'isDeleted',
                            jsonmap: 'msg.d.isDeleted',
                            label: 'isDeleted',
                            resizable: true,
                            search: true
                        }
                    },

                      { name: "DateTimeInserted",
                          width: 20,
                          hidden: false,
                          datefmt: 'Y-m-d',
                          sorttype: 'date',
                          editable: false,
                          editrules: { index: 'DateTimeInserted',
                              jsonmap: 'DateTimeInserted',
                              label: 'DateTimeInserted',
                              resizable: true,
                              search: true
                          }
                      },

                    { name: "KeyID",
                        width: 5,
                        hidden: true,
                        key: false,
                        sorttype: 'int',
                        editrules: { index: 'KeyID',
                            jsonmap: 'KeyID',
                            hidedlg: true,
                            label: 'KeyID',
                            resizable: false,
                            search: false
                        }
                    }
                ],
    height: 400,
    shrinkToFit: true,
    pager: jQuery('#pager'),
    rowNum: 10,
    rowList: [10, 20, 30, 40, 50],
    sortname: 'ROWID',
    sortorder: "asc",
    viewrecords: true,
    imgpath: 'http://localhost/DNN5/js/jQuery/jqGrid-3.4.3/themes/sand/images',
    caption: 'X12 Trading Partners',
    viewrecords: true,
    gridComplete: function() {
        alert("i'm grid complete" + JSON.stringify(msg));
        //updates the new column order position
        $('table.scroll').tableDnDUpdate();
    }}).navGrid('table.scroll', { edit: true, add: true, del: true }).tableDnD();

这是返回的JSON字符串:(看到这些数据返回给客户端我没有问题。)

成功:

{total:1,page:1,records:5,[{"ROWID":1,"ID":1,"PartnerID":"BCN","Direction":"I","InterchangeVersion":"004010X096A1","InterchangeSenderID":"BCC4010","InterchangeReceiverID":"company","ProductionMode":"T","SubmitterName":"BCC","SubmitterID":"BCC4010","SubmitterPOC":"BCC","CommQual":"","CommNumber":"","ReceiverName":"company","ReceiverID":"BCC4010","PartnerTypes":"","PartnerNotes":"","IncomingDataPrepSP":"","OutgoingDataPrepSP":"","ExchangeAck":false,"DateTimeInserted":new Date(1214412777787),"IsDeleted":false},{"ROWID":2,"ID":2,"PartnerID":"BCN","Direction":"I","InterchangeVersion":"004010X098A1","InterchangeSenderID":"BCC4010","InterchangeReceiverID":"company","ProductionMode":"T","SubmitterName":"BCC","SubmitterID":"BCC4010","SubmitterPOC":"BCC","CommQual":"","CommNumber":"","ReceiverName":"company","ReceiverID":"BCC4010","PartnerTypes":"","PartnerNotes":"","IncomingDataPrepSP":"","OutgoingDataPrepSP":"","ExchangeAck":false,"DateTimeInserted":new Date(1212088125000),"IsDeleted":false},{"ROWID":3,"ID":3,"PartnerID":"BCN","Direction":"O","InterchangeVersion":"004010X091A1","InterchangeSenderID":"HCA770385729","InterchangeReceiverID":"BCC47198       ","ProductionMode":"T","SubmitterName":"company Administrators","SubmitterID":"HCA770385729","SubmitterPOC":"briana","CommQual":"EM","CommNumber":"briana@company.com","ReceiverName":"BCN","ReceiverID":"BCC47198","PartnerTypes":"","PartnerNotes":"","IncomingDataPrepSP":"","OutgoingDataPrepSP":"","ExchangeAck":false,"DateTimeInserted":new Date(1212088125547),"IsDeleted":false},{"ROWID":4,"ID":4,"PartnerID":"EHG","Direction":"I","InterchangeVersion":"004010X097A1","InterchangeSenderID":"330989922","InterchangeReceiverID":"company","ProductionMode":"T","SubmitterName":"DENTALCONNECT","SubmitterID":"330989922","SubmitterPOC":"","CommQual":"","CommNumber":"","ReceiverName":"","ReceiverID":"","PartnerTypes":"Clearinghouse","PartnerNotes":"Dental clearinghouse.  Sends billing address in the pay-to segment (NM1*87) and the service address in the billing segment (NM1*85).","IncomingDataPrepSP":"[dispatch].spPreprocessEHG","OutgoingDataPrepSP":"","ExchangeAck":true,"DateTimeInserted":new Date(1235603192033),"IsDeleted":false},{"ROWID":5,"ID":5,"PartnerID":"EMDEON","Direction":"I","InterchangeVersion":"004010X097A1","InterchangeSenderID":"341884003","InterchangeReceiverID":"857297703","ProductionMode":"T","SubmitterName":"INTERACTIVE PAYER NETWORK","SubmitterID":"341884003","SubmitterPOC":"","CommQual":"","CommNumber":"","ReceiverName":"","ReceiverID":"","PartnerTypes":"Clearinghouse","PartnerNotes":"Dental clearinghouse.  Sends billing address in the pay-to segment (NM1*87) and the service address in the billing segment (NM1*85).","IncomingDataPrepSP":"","OutgoingDataPrepSP":"","ExchangeAck":true,"DateTimeInserted":new Date(1235603192000),"IsDeleted":false}]}

4 个答案:

答案 0 :(得分:2)

缺少该列表的标识符:

return_value = {
    total:1,
    page:1,
    records:5,
    [{  .. some X12 messages  ..}]
}

答案 1 :(得分:2)

我花了很多时间试图让jqGrid与使用JSON的ASP.NET一起工作。现在,我有它的工作,我想与你分享。希望能为其他人节省时间。

我找到了答案here。这并不是说没有其他方法可以完成这项工作(我确信有)。但这个肯定有效。

总结那些对我有所帮助的事情:

  • 在jqGrid的设置上使用“datatype:function(){”,而不使用“datatype:json”。该函数使用“$ .ajax(”来调用服务。查看Fiddler,这改变了返回浏览器的数据,变为纯JSON(而不是用XML包装的JSON)。
  • 使用返回的Web服务 数据类(在架构jqGrid中 预计)。我首先尝试使用JSON.NET 但出于某种原因,这个消息 显示(再次在Fiddler) 为了每一个引用而逃脱\ 串。我很确定你能得到 JSON.NET工作。但它只是工作 没有它。

答案 2 :(得分:1)

看起来您没有添加rows属性标识符。尝试更改:

json.Append(JsonConvert.SerializeObject(objPageCollection))

json.Append("rows: " + JsonConvert.SerializeObject(objPageCollection))

如果这不起作用,我建议您简化问题 - 例如将数据减少到一行 - 看看是否可以使用更简单的数据,然后逐渐将更多的东西添加回其中

答案 3 :(得分:1)

实际上, tvanfosson 对“行”标识符的要求提出了一个很好的观点。我不熟悉 jMSAjax 所以我的评论可能无效。我很高兴知道它。


它可以像返回类型一样简单吗?也就是说,当评估响应时,最终得到封装的字符串而不是json对象。

我自己也去过类似的地方。我发现选项要么是,创建一个表示预期的jqGrid响应的业务对象,并返回对象而不是字符串(ASP.NET会将它序列化为JSON给你!)或者将jqGrid修改为 eval 响应两次(凌乱)。