2

How do you set the userdata in the controller action. The way I'm doing it is breaking my grid. I'm trying a simple test with no luck. Here's my code which does not work. Thanks.

       var dataJson = new
        {

            total = 
            page = 1,
            records = 10000,
            userdata = "{test1:thefield}",
            rows = (from e in equipment
                    select new
                    {
                        id = e.equip_id,
                        cell = new string[] {
                e.type_desc,
                e.make_descr,
                e.model_descr,
                e.equip_year,
                e.work_loc,
                e.insp_due_dt,
                e.registered_by,
                e.managed_by
            }
                    }).ToArray()
        };
        return Json(dataJson);
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
MikeD
  • 741
  • 5
  • 20
  • 38
  • For those who (like me) are new to jqGrid and wondering what userdata is, see http://stackoverflow.com/questions/3128837/json-and-jqgrid-what-is-userdata – Chris Kimpton Jun 28 '10 at 10:24

1 Answers1

2

I don't think you have to convert it to an Array. I've used jqGrid and i just let the Json function serialize the object. I'm not certain that would cause a problem, but it's unnecessary at the very least.

Also, your user data would evaluate to a string (because you are sending it as a string). Try sending it as an anonymous object. ie:

userdata = new { test1 = "thefield" },

You need a value for total and a comma between that and page. (I'm guessing that's a typo. I don't think that would compile as is.)

EDIT: Also, i would recommend adding the option "jsonReader: { repeatitems: false }" to your javascript. This will allow you to send your collection in the "rows" field without converting it to the "{id: ID, cell: [ data_row_as_array ] }" syntax. You can set the property "key = true" in your colModel to indicate which field is the ID. It makes it a lot simpler to pass data to the grid.

Josh
  • 1,001
  • 9
  • 18
  • Thanks for the response - Now I am having issue reading this value on the client. Is it not - var uData = jQuery('#equipgrid').getGridParam('userdata'); alert(uData.test1); – MikeD Dec 22 '09 at 16:01
  • Looks like according to the docs, you'd get the value using $("grid_id").getGridParam('userData'); (Notice the capital "D".) This is totally inconsistent with the "userdata" param in the jsonReader object, but i've noticed many inconsistencies like that within jqGrid. – Josh Dec 22 '09 at 21:12
  • I just noticed in the docs you can also get the value via $("grid_id").getUserData(); or $("grid_id").getUserDataItem(key); - that's probably a more intuitive way to retrieve the value. – Josh Dec 22 '09 at 21:14
  • Thanks Josh that was a big help. Turns out the capital "D" was my problem on the read. – MikeD Dec 23 '09 at 15:29