2013-11-29 91 views
-1

我在控制器中有以下方法,我从我的视图调用填充jqgrid。此方法工作正常并返回数据。如何将jsonResult对象绑定到jqgrid?

public JsonResult _FirstLook() 
{ 
    HttpResponseMessage response; 

    response = client.GetAsync("api/CasoAdverso").Result; 
    if (response.IsSuccessStatusCode) 
     { 
      IEnumerable<CasoAdverso> list = response.Content.ReadAsAsync<IEnumerable<CasoAdverso>>().Result; 

      return Json(list); 
     } 
} 

如果我调试正在返回的结构将如下图所示。对象列表中的数据:

enter image description here

我将总结CasoAdverso类,因为它是相当大的投入在这里:

public class CasoAdverso 
{ 
    public int CAAD_Id { get; set; } 
    public string CAAD_Id_Local { get; set; } 
    public System.DateTime? CAAD_Fecha_Contacto { get; set; } 
} 

实际接收的数据来填充,但不知何故jqGrid的不正在显示:

$(grid_selector).jqGrid({ 
datatype: "json", 
height: 250, 
mType: 'GET', 
url: "@Url.Action("_FirstLook", "CasoAdversoForm")", 
colNames: ['ID', 'ID Caso', 'Fecha Contacto Notif.'], 
     colModel: [ 
      { name: 'CAAD_Id', index: 'CAAD_Id', key: true }, 
      { name: 'CAAD_ID_Local', index: 'CAAD_ID_Local', width: 60, editable: false }, 
      { name: 'CAAD_Fecha_Contacto', index: 'CAAD_Fecha_Contacto', width: 90, editable: false, sorttype: "date", unformat: pickDate }, 
     ], 
    ... 
     }, 

我知道该解决方案可能通过jqReader中的jqgrid,但我不能做任何p没有完全改变控制器中的_FirstLook方法。

在我的方案中,我需要更改jqgrid来绑定我目前从控制器获取的内容。在控制器中,不必为jqgrid默认需要做一些解决方法。

+0

请定义“绑定”:期望的行为是什么?它与实际行为有什么不同? – Saturnix

+0

在这种情况下,绑定将设置jsonReader,它将网格所期望的结构与JsonResult对象的结构“链接”。 预期的行为将填充和显示,但这不会发生...... – Javier

+0

这里有一个jsonreader的例子:http://stackoverflow.com/questions/14748169/jqgrid-jsonreader-configuration – Javier

回答

2

我想所描述的问题的主要原因不是“绑定”。您使用mType: 'GET'选项,因为名称mType的选项不存在,所以将被忽略。因此将使用默认选项值mtype: 'GET'mtype而不是mtype)。所以,你应该要么使用mtype: 'POST'或更换线路

return Json(list); 

return Json(list, JsonRequestBehavior.AllowGet); 

此外,我会建议您确认您使用loadonce: true选项,因为你没有在你的服务器代码中实现服务器端分页。我建议您另外使用gridview: true,autoencode: true选项,并考虑使用height: "auto"而不是height: 250