Reputation: 121
I just started working with jqGrid this week. I'm attempting to make a generic function that I can call to load a jqGrid with different postData parameters, defined in the function call.
Is there any way to do this? What I had considered was creating an array to store the parameter's name/value, then setting that as a parameter in the postData. The web service would then go through and add those parameters to the SQL stored procedure call.
Here is the generic code:
var paramArray = new Array();
var p1 = new JQGridParam("CustomerName","John");
var p2 = new JQGridParam("ProductName","Kleenex");
var p3 = new JQGridParam("YearPurchased","2012");
paramArray[0] = p1;
paramArray[1] = p2;
paramArray[2] = p3;
$("#list").jqGrid({
datatype: "json",
url: "services/Customers.asmx/GetCustomerData",
mtype: "POST",
postData: {
sqlParams: paramArray //HERE IS WHERE I NEED THE HELP
},
ajaxGridOptions: { contentType: "application/json; charset=utf-8" },
ajaxRowOptions: { contentType: "application/json; charset=utf-8" },
serializeGridData: function (postData) {
return JSON.stringify(postData);
},
serializeRowData: function (postData) {
return JSON.stringify(postData);
},
jsonReader: {
root: function (obj) { return obj.d.rows; },
page: function (obj) { return obj.d.page; },
total: function (obj) { return obj.d.total; },
records: function (obj) { return obj.d.records; },
},
colModel: [
{name: "CustID", index: "CustID" },
{name: "PurchaseAmount", index: "PurchaseAmount" }],
colNames: ["Customer ID", "Amount Purchased"],
pager: $("#pager"),
loadOnce: true,
rowNum: 10,
rowList: [10,25,50],
gridview: true,
viewrecords: true
});
When I try this, it throws the error "cannot convert object of type System.String to type System.Array". Anyone have any ideas how to go about this?
Upvotes: 3
Views: 5127
Reputation: 421
JSON.stringify didn't work for me using MVC .NET with C#. Instead, I just serialized the form and sent the data as part of the URL:
$("#myGrid").jqGrid({
url: '<%=Url.Action("MyControllerMethod", "MyController") %>' +
'?' + $('#myForm').serialize(),
I left mtype as "POST" for this workaround. Using postData, I was able to get individual numbers or strings to bind just fine, but not arrays. This workaround will allow you to use arrays with C# MVC.
Upvotes: 0
Reputation: 3091
JSON stringify the array.
JSON.stringify(paramArray);
Then in your webservice cast the JSON data back to your object.
Upvotes: 3