I worked on sending all rows from jqGrid to an action method, using the general idea from this question , and some code from jqGrid Wiki comments. Essentially in submit, I want ALL the string data to return to the controller so that I can save it. I tried using a hidden field to store all the row data, but the controller never gets everything, only the last edited cell in the grid. So I switched to ajax approach, but no matter what I tried, I can't get ajax POST
to meet JSON. Here is 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)
I tried changing the gridData
parameter from string
to string[]
to object[]
to all kinds of things, and nothing works. If I leave it as a string
, I get the data, but the format is all stupid, for example (column names replaced):
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="]
I got this from Fiddler, and for writing, the JSON tab shows nothing for the request when I squeak it. Is there a way I can use JSON-ify for this array and send it in a single call? What type will my parameter be in my action method?
EDIT - solution
For any other people who are just as stupid as I am, here is how I got this to work:
First, at the suggestion of Oleg, I added loadonce: true
to the jqGrid definition. Then we changed the message sending 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 the signature of my controller changed:
public ActionResult UpdateAwesomeGridData(IEnumerable<GridBoundViewModel> gridData)
Hope this helps someone in the future.