Extjs Gridview显示无数据

时间:2012-09-25 17:06:28

标签: sql asp.net-mvc json gridview extjs

我正在尝试使用json在extjs中创建gridview。出于某种原因,我的gridview没有显示任何数据。我试着用firebug调试它。我可以在“响应”部分看到结果。 这就是我在“Reponse”中所拥有的。

  

{“ContentEncoding”:null,“ContentType”:null,“Data”:“{\ r \ n \”myTable \“:[\ r \ n {\ r \ n \”\ Q1 \“:\” 1 \“,\ r \ n \”Q2 \“:\”1 \“,\ r \ n \”Q3 \“:\”1 \“,\ r \ n \”Q4 \“:\”1 \ “,\ r \ n \”改进\“:\”\“,\ r \ n \”评论\“:\”1 \“\ r \ n},\ r \ n {\ r \ n \”Q1 \“:\”1 \“,\ r \ n \”Q2 \“:\”2 \“,\ r \ n \”Q3 \“:\”3 \“,\ r \ n \”Q4 \“ :\“4 \”,\ r \ n \“改进\”:\“Iphone5 \”,\ r \ n \“评论\”:\“Iphone14 \”\ r \ n},\ r \ n \ n \ r \ n \“Q1 \”:\“1 \”,\ r \ n \“Q2 \”:\“1 \”,\ r \ n \“Q3 \”:\“3 \”,\ r \ n \“Q4 \”:\“3 \”,\ r \ n \“改进\”:\“这是Comment1-3 \”,\ r \ n \“评论\”:\“这是评论2-3 \“\ r \ n} \ r \ n] \ r \ n}”,“JsonRequestBehavior”:0}

更新 的 其实我现在在Json看到这个,但我的gridview仍然是空的 Please click here to see my JSON

/GridViewApp.js

Ext.define('GridViewApp.view.GridViewApp', {
alias: 'widget.gridviewapp',
width: 800,
title: 'My Grid Panel',
grid: null,
store: null,
layout: {
    type: 'anchor'
},
constructor: function () {

    this.callParent(arguments);

    var store = Ext.create('Ext.data.Store', {

        storeId: 'myData',
        scope: this,
        fields: [
        { name: 'Q1', type: 'int' },
        { name: 'Q2', type: 'int' },
        { name: 'Q3', type: 'int' },
        { name: 'Q4', type: 'int' },
        { name: 'Q5', type: 'int' },
        { name: 'Improvements', type: 'string' },
        { name: 'Comments', type: 'string' }
        ],

        sorters: [
            {
                //property: 'myData',
                direct: 'ASC'
            }
         ],

        proxy: {
            type: 'ajax',
            scope: this,
            url: 'GridView/writeRecord',
            reader: {
                type: 'json',
                root: 'myTable',
                idProperty: 'ID'

                }
        } 
    });

    store.load();   
    this.grid = Ext.create('Ext.grid.Panel', {
        title: 'GridView App',
        store: this.store,
        columns: [
            {header: 'Q1', width: 100,
        sortable: true, dataIndex: 'Q1'
                    },
        { header: 'Q2', width: 100,
            sortable: true, dataIndex: 'Q2'
        },
        { header: 'Q3', width: 100,
            sortable: true, dataIndex: 'Q3'
        },
                    { header: 'Q4', width: 100,
                        sortable: true, dataIndex: 'Q4'
                    },
                    { header: 'Improvements', width: 200,
                        sortable: true, dataIndex: 'Improvements'
                    },
                    { header: 'Comments', width: 200,
                        sortable: true, dataIndex: 'Comments'
                    }
    ],
        stripeRows: true,
        width: 800,
        renderTo: Ext.getBody()
    });
    this.add(this.grid);
}
});

和/GridViewController.cs

namespace GridViewApp.Controllers
{
public class GridViewController : Controller
{

    public ActionResult Index()
    {
        return View();
    }


    public JsonResult writeRecord()
    {

        SqlConnection conn = DBTools.GetDBConnection("ApplicationServices2");


        string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable";
        SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn);

        DataSet myData = new DataSet();
        cmd.Fill(myData, "myTable");

        conn.Open();

        conn.Close();


        string myData1 = JsonConvert.SerializeObject(myData, Formatting.Indented,
                        new JsonSerializerSettings
                        {
                            ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                        });

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

}
}

任何形式的输入都将是一个很大的帮助......谢谢

1 个答案:

答案 0 :(得分:0)

它返回一个字符串是非常合乎逻辑的。

您使用json.net序列化为字符串。然后用Json()序列化该字符串。您应该只使用json.net序列化。

public string writeRecord()
{

    SqlConnection conn = DBTools.GetDBConnection("ApplicationServices2");


    string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable";
    SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn);

    DataSet myData = new DataSet();
    cmd.Fill(myData, "myTable");

    conn.Open();

    conn.Close();


    return myData1 = JsonConvert.SerializeObject(myData, Formatting.Indented,
                    new JsonSerializerSettings
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    });

}