JSON数据未在JQGrid中显示

时间:2014-10-06 21:06:21

标签: c# asp.net json oracle jqgrid

我看过所有相关答案中的建议,但无济于事。我尝试了各种选择,但再次无济于事。我使用 ASP.NET with C#作为处理语言, Oracle 10g 作为数据库引擎。

这是我的.aspx文件:

<form id="LocalizationSearch" name="locSearch" defaultbutton="updateButton" runat="server">
   <script language="javascript" type="text/javascript">
    jQuery(document).ready(function () {
        var createLanguageList = function () {
            $("#list").jqGrid({
                url: "KPHandler.ashx?langname=English",
                datatype: "json",
                contentType: "application/json; charset=utf-8", 
                mtype: 'POST',
                //datatype: "local",
                //data: mydata,
                autowidth: true,
                height: 150,
                colNames: ['DelLanguageID', 'DocRecID', 'LanguageID', 'DocPartNumber,', 'NewLanguage', 'ConcentraIDLang', 'ActFromTranslation'],
                colModel: [
                    { name: 'DelLanguageID', index: 'DelLanguageID', width: 65, editable: false },
                    { name: 'DocRecID', index: 'DocRecID', width: 65, editable: false },
                    { name: 'LanguageID', index: 'LanguageID', width: 65, editable: false },
                    { name: 'DocPartNmbr', index: 'DocPartNmbr', width: 65, editable: true },
                    { name: 'NewLanguage', index: 'NewLanguage', editable: true, edittype: 'checkbox', editoptions: { value: "1:0"} },
                    { name: 'ConcentraIDLang', index: 'ConcentraIDLang', width: 65, editable: true },
                    { name: 'ActFromTranslation', index: 'ActFromTranslation', width: 120, editable: true }
                ],
                rowNum: 5,
                rowList: [10, 20, 30],
                pager: '#pager',
                sortname: 'DelLanguageID',
                viewrecords: true,
                //altRows: true,
                //sortorder: "desc",
                caption: "Deliverable Languages",
                //height: "100%",
                loadonce: true, 
                gridview: true, 
                autoencode: true,
                loadError: function (jqXHR, textStatus, errorThrown) {
                    alert('Error: ' + textStatus + errorThrown + jqXHR.responseText);
                }
            });  // JQGrid
        };  // Create function

        $("#btnLanguages").click(function () {
            createLanguageList();
        });

    });   // document ready 

</script>

    <table cellspacing="1" cellpadding="0" width="100%">
        <tr>
            <td valign="top">
                <table id="list"></table>
                <div id="pager"></div>
        </td>
        </tr>
    </table>
    <input type="button" id="btnLanguages" value="Work With Languages"  />

</form>

这是构建JSON对象的C#代码:

<%@ WebHandler Language="C#" Class="KPHandler" %>

using System;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Configuration; 
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OracleClient;

public class KPHandler : IHttpHandler
{
    private DKCL.ReportDK Rpt = new DKCL.ReportDK();

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        System.Collections.Specialized.NameValueCollection forms = context.Request.Form;
        string strOperation = forms.Get("oper");

        switch (strOperation)
        {
            case null:  // means this is the first time load
                string strSQL = "SELECT DelLanguageID, DocRecID, LanguageID, DocPartNmbr, NewLanguage, " +
                                "ConcentraIDLang, ActFromTranslation FROM DELLANGUAGES WHERE DOCRECID = 18787";

                DataTable dlLanguages = Rpt.GetDataTable(strSQL);

                context.Response.Write(GetJson(dlLanguages));

                break;
            default:
                break;
        }  // End Switch

    }

    // This is generic function that converts DataTable into JSON object.
    public string GetJson(DataTable dt)
    {
        System.Web.Script.Serialization.JavaScriptSerializer serializer = new

        System.Web.Script.Serialization.JavaScriptSerializer();
        List<Dictionary<string, object>> rows =
          new List<Dictionary<string, object>>();
        Dictionary<string, object> row = null;

        foreach (DataRow dr in dt.Rows)
        {
            row = new Dictionary<string, object>();
            foreach (DataColumn col in dt.Columns)
            {
                row.Add(col.ColumnName.Trim(), dr[col]);
            }
            rows.Add(row);
        }
        return serializer.Serialize(rows);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}

这里是上面代码返回的JSON数据(我实际上是由Fiddler2显示的),我自己调试代码:

            [{"DELLANGUAGEID":"4613","DOCRECID":"18787","LANGUAGEID":"12","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02609839","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4614","DOCRECID":"18787","LANGUAGEID":"34","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4615","DOCRECID":"18787","LANGUAGEID":"1","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02606925","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4616","DOCRECID":"18787","LANGUAGEID":"13","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02609850","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4617","DOCRECID":"18787","LANGUAGEID":"11","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02606924","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4618","DOCRECID":"18787","LANGUAGEID":"3","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":"c02606920","ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"4619","DOCRECID":"18787","LANGUAGEID":"21","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"39703","DOCRECID":"18787","LANGUAGEID":"142","DOCPARTNMBR":"593957-001","NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"89446","DOCRECID":"18787","LANGUAGEID":"161","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"97174","DOCRECID":"18787","LANGUAGEID":"162","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"112630","DOCRECID":"18787","LANGUAGEID":"164","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null},{"DELLANGUAGEID":"104902","DOCRECID":"18787","LANGUAGEID":"163","DOCPARTNMBR":null,"NEWLANGUAGE":0,"CONCENTRAIDLANG":null,"ACTFROMTRANSLATION":null}]

知道可能出现什么问题吗? 网格显示正确的记录数量以及寻呼机中的页面。任何帮助都将非常受欢迎。
顺便说一句,我尝试了GET vs POST,json vs jsonstring等以及许多其他选项,包括JSON Reader,但没有运气。

2 个答案:

答案 0 :(得分:0)

colModel索引属性旨在完全反映JSON结果中的键。请参阅documentation

  

index :传递给服务器的名称,用于对数据进行排序(请注意,我们可以传递列号)。通常,这是数据库中的名称(或名称) - 这是服务器端排序,因此您传递的内容取决于服务器期望接收的内容。

鉴于该信息,示例中的索引看起来不正确。尝试将它们纠正为正确的大小写或使用数值。

示例:

colModel: [{ name: 'DelLanguageID', index: 'DELLANGUAGEID', width: 65, editable: false },...

答案 1 :(得分:0)

谢谢Strake。这对我来说很痛苦,但很有教育意义。现在只需更改** ColModel **就可以了。再次感谢。