将计数和数据作为json返回到extjs网格中

时间:2012-09-26 19:03:00

标签: c# sql asp.net-mvc count json

如何返回两个Json个对象?基本上我试图从sql db发送一堆数据和db中的行数。

以下代码成功发送myData,但现在我还想发送count

string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable";
conn.Open();
SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn);
SqlCommand comd = new SqlCommand(sqlquery, conn);
DataSet myData = new DataSet();
cmd.Fill(myData, "myTable");


comd.CommandText = "SELECT COUNT(*) FROM myTable";
Int32 count = (Int32)comd.ExecuteScalar();

comd.ExecuteNonQuery();
conn.Close();

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

这是我的网格

this.grid = Ext.create('Ext.grid.Panel', {
        title: 'GridView App',
        frame: true,
        trackMouseOver: true,
        disableSelection: true,
        autoHeight: true,
        store: store,
        loadMask: true,
        columns: [

        { header: 'Q1',
            sortable: true, dataIndex: 'Q1'
        },
        { header: 'Q2',
            sortable: true, dataIndex: 'Q2'
        },
        { header: 'Q3',
            sortable: true, dataIndex: 'Q3'
        },
        { header: 'Q4',
            sortable: true, dataIndex: 'Q4'
        },
        { header: 'Improvements', flex: 1,
            sortable: true, dataIndex: 'Improvements'
        },
        { header: 'Comments', flex: 1,
            sortable: true, dataIndex: 'Comments'
        }
    ],

这是我的商店...

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

        storeId: 'myData',
        scope: this,
        pagesize: itemsPerPage,
        remoteSort: true,
        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: 'Q1',
            direct: 'ASC'
        }
     ],

        proxy: {
            type: 'ajax',
            scope: this,
            url: 'GridView/writeRecord',
            reader: {
                type: 'json',
                root: 'myTable',
                totalProperty: 'count'
            }
        }

    });

2 个答案:

答案 0 :(得分:0)

这是我的工作方式,我将它用于我的所有mvc应用程序。我在互联网上找到了这个例子(我稍微改了一下),因为我在办公室,只在这里使用webforms(我知道......那很糟糕,当你去mvc时你永远不会回去)

public class Person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }


public class HomeController : Controller
    {

        public List<Person> personList = new List<Person>{ 
                                                        new Person{ Name="Jpnh", Surname="Coco"},
                                                        new Person{ Name="Mike", Surname="Nile"}  
                                                      };

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



        public ActionResult GetData()
        {

            return Json(personList);
        }

}

 <script type="text/javascript" language="javascript">

        $(document).ready(function () {

            $('#button').click(GetValue);

        });

        function GetValue() {
            $.ajax({
                url: "/Home/GetData",
                success: function (data) {
                    alert(data[0].Name);
                }

            });

        };

    </script>

代码本身非常直观易懂,我假设您从数据库中填写通用列表没有问题

让我知道

答案 1 :(得分:0)

这基本上就是我在ASP.NET应用程序中的表现,我也在使用JSON.NET:

string sqlquery = "SELECT Q1, Q2, Q3, Q4, Improvements, Comments FROM myTable";
conn.Open();
SqlDataAdapter cmd = new SqlDataAdapter(sqlquery, conn);
SqlCommand comd = new SqlCommand(sqlquery, conn);
DataSet myData = new DataSet();
cmd.Fill(myData, "myTable");


comd.CommandText = "SELECT COUNT(*) FROM pid_FluSurvey";
Int32 count = (Int32)comd.ExecuteScalar();

comd.ExecuteNonQuery();
conn.Close();

return JsonConvert.SerializeObject( new { myTable = myData.Tables[0], count = count }, Formatting.Indented,
    new JsonSerializerSettings
    {
        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
    });

这里重要的是你可以在任何匿名类型上调用SerializeObject并在那里放置你想要/需要的任何数量的属性。