views:

204

answers:

2

Trying to get a JSON output to work with jqGrid 'userdata' option.

The example given in the jqGrid PDF is to produce a JSON result like this;

{   total: "xxx",  
    page: "yyy", 
    records: "zzz",  
    userdata: {totalinvoice:240.00, tax:40.00}, 
    rows : [ 
        {id:"1", cell:["cell11", "cell12", "cell13"]}, 
        {id:"2", cell:["cell21", "cell22", "cell23"]}, 
    ] 
}

I have tried to make the userdata portion like this (with no luck);

 var jsonData = new 
 { ...
   userdata = new string[] {"totalinvoice:240.00", "tax:40.00" }
   ...}
 return Json(jsonData);

But that comes back in the JSON results looking like this;

userdata":["totalinvoice:240.00","tax:40.00"]

I presume this is something trivial, but I cant seem to figure out how to get the System.Web.JSON.JsonResult call to return the value as expected by jqGrid.

A: 

why don't you use the jquery plugin "toJson"?: http://jollytoad.googlepages.com/json.js
http://docs.jquery.com/Plugins (please search: toJson)

MemoryLeak
I have updated it.
MemoryLeak
+2  A: 

Try:

var jsonData = new 
 { ...
   userdata = new { totalinvoice = 240.00, tax = 40.00 }
   ...}
 return Json(jsonData);

In JSON notation, { } is for objects, [ ] is for arrays. If it's expecting something in curly braces, you should serialize an object with those properties.

womp
woot! Right on and it worked.
CmdrTallen