2011-08-18 51 views
1

我在多选模式下使用JqGrid并恢复用户的选择。 JqG​​rid的定义如下:ASP.NET MVC/JqGrid:JSON Id是否已处理并可恢复?

$("#StatusList").jqGrid({ 
     url: '@Url.Action("UpdateStatusList", "Inventory")', 
     datatype: 'json', 
     mtype: 'GET', 
     colNames: ['Status', 'statusId'], 
     colModel: [ 
      { name: 'Status', index: 'Status', align: 'left', sortable: false }, 
      { name: 'statusId', index: 'statusId', hidden: true}] 
}); 

我对服务器和客户端之间的所有通信没有任何问题。一切正常。但我觉得这个功能发送JSON数据填充的jqGrid的时候我好像复制数据:

public ActionResult UpdateStatusList() 
    { 
    var jsonData = new 
    { 
     rows = (from status in DatabaseInMemory.WoodStatus.GetEntities() 
       select new 
       { 
        i = status.ID, 
        cell = new string[] { status.Name, 
           status.ID.ToString() } 
       }).ToArray() 
    }; 
    return Json(jsonData, JsonRequestBehavior.AllowGet); 
    } 

正如你所看到的ID传递两次: - 为JSON标识。 - 用于帮助我从网格中恢复ID的隐藏列。

回到客户端,JSON ID不在selarrrow属性中。该属性拥有基于网格中位置的ID。我用它来获取选定的数据,并恢复真正的ID。

JqGrid是否处理并保存通过JSON数据传递的id,或者是否丢失,并且始终需要隐藏的列来跟踪行?

回答

2

您应该将i = status.ID更改为id = status.ID

+0

发布了一个重构命题,用于根据您的回答创建我的网站的示例:http://www.timdavis.com.au/code/jquery-grid-with-asp-net-mvc/。谢谢:) – Matthieu

+0

@Matthieu:看看[答案]的更新部分(http://stackoverflow.com/questions/5500805/asp-net-mvc-2-0-implementation-of-searching-in-jqgrid/ 5501644#5501644)。您可以下载[VS2008演示项目](http://www.ok-soft-gmbh.com/jqGrid/jqGridDemo.zip)或[VS2010演示项目](http://www.ok-soft-gmbh.com /jqGrid/jqGridDemoVS2010.zip)。你可以在[页面](http://haacked.com/archive/2009/04/14/using-jquery-grid-with-asp.net-mvc.aspx)上搜索“Oleg”并找到我的错误报告。这些错误是在菲尔哈克的页面上修复的,但不是蒂姆戴维斯的演示副本。 – Oleg

+0

不错,将来会用你的答案作为参考,thx! – Matthieu

相关问题