从视图中提交的jqGrid行数据到控制器 - 有什么方法? [英] Submitting jqGrid row data from view to controller - what method?

查看:144
本文介绍了从视图中提交的jqGrid行数据到控制器 - 有什么方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用从的这个问题并从一些评论code上的 jqGrid的维基。从本质上讲,对提交的,我希望所有的数据行要回控制器,所以我可以坚持它。我用一个隐藏字段来存储所有的行数据的尝试,但控制器似乎从来没有得到的一切,只有在网格中的最后一个编辑的单元格。所以,我切换到Ajax方法,但无论怎样我试过,我不能得到AJAX POST 给人以JSON。下面是我现在:

I've been working on submitting all of the rows from a jqGrid to an action method using the general idea from this question and some code from the comments on the jqGrid Wiki. Essentially, on submit, I want ALL of the row data to get back to the controller so I can persist it. I've tried using a hidden field to store all of the row data, but the controller never seems to get everything, only the last edited cell in the grid. So I switched to an ajax approach, but no matter what I've tried I can't get the ajax POST to come across as JSON. Here's what I have right now:

$("#submitButton").click(function () {
    $("#awesomeGrid").jqGrid('resetSelection');
    var gridRows = $("#awesomeGrid").jqGrid('getRowData');
    var rowData = new Array();
    for (var i = 0; i < gridRows.length; i++) {
        var row = gridRows[i];
        rowData.push($.param(row));
    }
    var dataToSend = JSON.stringify(rowData);
    $.ajax({
        url: '@Url.Action("UpdateAwesomeGridData")',
        type: 'POST',
        data: { gridData: dataToSend },
        dataType: 'json',
        success: function (result) {
            alert('success');
        }
    });
    return true;
});

和我的控制器方法签名:

And my controller method signature:

[HttpPost]
public ActionResult UpdateAwesomeGridData(string gridData)

我试过从字符串修改的GridData 参数的String [] [对象] 来各种东西,并且似乎没有任何工作。如果我离开它,因为字符串,我得到的数据,但格式是所有古怪,像这样(列名取代):

I've tried changing the gridData parameter from string to string[] to object[] to all sorts of stuff, and nothing seems to work. If I leave it as string, I get the data, but the format is all wacky, like this (column names substituted):


gridData=[\"Id=1&Field1=1945&Field2=0&Field3=0&Field4=1&Field5=Some+string+value&Field6=&Field7=&Field8=&Field9s=\",\"Id=2&Field1=1945&Field2=0&Field3=0&Field4=2&Field5=Another+string+value&Field6=&Field7=&Field8=&Field9s=\",\"Id=3&Field1=1945&Field2=0&Field3=0&Field4=3&Field5=Yet+another+string&Field6=&Field7=&Field8=&Field9s=\",\"Id=4&Field1=1945&Field2=0&Field3=0&Field4=4&Field5=Final+string&Field6=&Field7=&Field8=&Field9s=\"]

我得到了这一点提琴手,并作记录,JSON的标签显示什么时,我拨弄它的要求。有没有一种方法可以让我JSON-IFY此数组,并在一个单一的通话发呢?将我的参数在我的操作方法是什么类型的?

I got this out of Fiddler, and for the record, the JSON tab shows nothing for the request when I fiddle it. Is there a way I can JSON-ify this array and send it in a single call? What type would my parameter be on my action method?

编辑 - 解决方案

对于那些哑巴,因为我任何其他人,这里就是我得到了它的工作:

For any other people that are as dumb as I am, here's how I got it to work:

首先,对奥列格的建议下,我加入 loadonce:真正的来的jqGrid的定义。然后,如下改变了我的提交按钮功能:

First, on Oleg's suggestion, I added loadonce: true to the jqGrid definition. Then, changed my submit button function as follows:

$("#submitButton").click(function () {
    var griddata = $("#awesomeGrid").jqGrid('getGridParam', 'data');
    var dataToSend = JSON.stringify(griddata);
    $.ajax({
        url: '@Url.Action("UpdateAwesomeGridData")',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: dataToSend,
        dataType: 'json',
        success: function (result) {
            alert('success: ' + result.result);
        }
    });
    return true;
});

然后,改变了我的控制器方法签名:

Then, changed my controller method signature:

public ActionResult UpdateAwesomeGridData(IEnumerable<GridBoundViewModel> gridData)

希望这可以帮助别人的未来。

Hope this helps someone in the future.

推荐答案

我看到你有很多问题。

第一个问题是您使用 $。参数(行),并填补了数组 rowData 带连接codeD(??? !!!)数据。我认为正确的code应包含 getRowData 返回的数据直接发帖:数据:{的GridData:gridRows} 。在服务器端,你可以使用 UpdateAwesomeGridData(字符串的GridData),然后再转换的GridData 清单&LT;名单,LT;串GT;&GT; 例如通过

The first problem is that you use $.param(row) and fill the array rowData with encoded (???!!!) data. I think the correct code should contain direct posting of the data returned from getRowData: data: { gridData: gridRows }. On the server side you can use UpdateAwesomeGridData(string gridData) and then convert gridData to the List<List<string>> for example by

JavaScriptSerializer serializer = new JavaScriptSerializer();
var myListOfData = serializer.Deserialize<List<List<string>>>(gridData);

我想去上一步,而提出这样的问题:为什么你需要将数据发送到它已经拥有的服务器?如果您需要某种目的从网格中的所有数据,你可以只使用控制器相同的动作而产生的数据,所以你将有相同的数据已经在服务器上。

将数据发送到服务器,可以根据需要只在一个场景:你使用 loadonce:真正的,修改的数据的本地的,并在年底修改你所需要的所有更改的数据发送到服务器。在这种情况下 getRowData 的用法是不是最好的选择,因为它让你从当前页面的数据的只有的。为了得到你应更好地使用数据 getGridParam(数据)

Sending data to the server can be needed only in one scenario: you used loadonce: true, modified the data locally and at the end of modifications you need send all the changed data to the server. In the case the usage of getRowData would be not the best choice because it get you the data from the current page only. To get all the data you should better use getGridParam("data").

这篇关于从视图中提交的jqGrid行数据到控制器 - 有什么方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆