views:

344

answers:

1

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);
+2  A: 

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
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
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
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
Thanks Josh that was a big help. Turns out the capital "D" was my problem on the read.
MikeD