2009-11-23 38 views
1

在我的應用程序中,我將在第一次加載頁面時從服務器端獲取數據表格,並使用jqgrid進行渲染。將數據數據傳遞到jqgrid表中

當我請求下面的例子的DummyView頁面時,Web瀏覽器彈出一條消息,要求我下載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的工作?

回答

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動詞。這是安全的,因爲返回的根數據不是數組。

0

如果你打電話來獲取數據,你可以使用jQuery;

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

我知道這迫使你做一個jQuery回發,但你不提這是一個問題。

+0

不知道你的答案如何解決我的問題;看到更新後的問題 – Graviton 2009-11-23 09:30:17