【问题标题】:Pass Data Arrays into jqgrid table将数据数组传递到 jqgrid 表中
【发布时间】:2009-11-23 02:12:16
【问题描述】:

在我的应用程序中,我会在页面第一次加载时从服务器端获取一个数据表,并使用 jqgrid 来呈现它。

我似乎无法让它工作,当我为下面的示例请求DummyView 页面时,网络浏览器弹出一条消息,要求我下载application/json 文件,表明有些事情是大错特错。这是我的 Jquery 代码:

 $(document).ready(function(){ 
      $("#list4").jqGrid({
        url:'../../ViewRecord/DummyView',
        datatype:'JSON',
        mtype: 'GET',
         colNames:['Id','Name','Description'],
 colModel:[
                         {name:'id',index:'id',width:55,resizable:true},
                         {name:'name',index:'name',width:90,resizable:true},
                        {name:'description',index:'description',width:120,resizable:true}],
        pager: jQuery('#pager'),
        rowNum:10,
        rowList:[5,10,20,50],
        sortname: 'Id',
        sortorder: "desc",
        viewrecords: true,
        imgpath:'/images',
        caption: 'My first grid'
      }); 
    });

这是我的ViewRecord/DummyView 控制器方法:

public ActionResult DummyView()
{
    var page = new { page = 1 };

    var rows = new object[2];
    rows[0] = new { id = 222, cell = new[] { "222", "Blue", "This is blue" } };
    rows[1] = new { id = 2, cell = new[] { "2", "Red", "This is red" } };

    //var endarray = new[] {page, rows};


    var result = new JsonResult();
    result.Data = new { page = 1, records = 2, rows, total = 1 };

    return result;


}

知道如何让 jqgrid 与 ASP.NET MVC 一起工作吗?

【问题讨论】:

    标签: jquery asp.net-mvc jqgrid


    【解决方案1】:

    我有一个完整的、有效的演示解决方案here。我还有一系列关于一起使用 MVC 和 jqGrid 的帖子,从 here 开始。演示解决方案包含LINQ extensions,因此您可以执行以下操作:

    public JsonResult ListGridData(int page, int rows, string search, string sidx, string sord)
    {
        var model = repository.SelectAll().ToJqGridData(page, rows, sidx + " " + sord, search,
          new[] { "Column1", "Column2", "Column3" })
        return Json(model);
    }
    

    请注意,对于 MVC 2,您必须包含 JsonRequestBehavior.AllowGet 才能使用 GET HTTP 动词。这是安全的,因为返回的根数据不是数组。

    【讨论】:

      【解决方案2】:

      如果你调用来获取数据,你可以使用jQuery;

      $(document).ready( function() {
          //your code here
      });
      

      我知道这会迫使您执行 jQuery 回发,但您没有提到这是一个问题。

      【讨论】:

      • 不确定您的回答如何解决我的问题;查看更新的问题
      猜你喜欢
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 2011-08-24
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多