1

如何返回两个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'
            }
        }

    });
4

2 回答 2

0

这就是我的做法,我将它用于我所有的 mvc 应用程序。我在互联网上找到了这个例子(我改变了一点),因为我在办公室并且只在这里使用网络表单(我知道......这很糟糕,当你去 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>

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

让我知道

于 2012-09-26T21:40:28.237 回答
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任何匿名类型并在那里放置您想要/需要的任意数量的属性。

于 2012-09-26T21:45:33.837 回答