Kendo Grid Dynamic Columns with ExpandoObjects

I read about ExpandoObjects in C# when it was first introduced but never saw the need. A few months ago I thought they could help in my dynamic columns on my Kendo Grid. I could pass just the data the user has setup to view.

See the following for more detail on the grid setup and link to Github example.

If I get time I setup a demo but only trouble I had was on the JSON formatting so here is that info....

Can make a list of ExpandoObjects like

 dynamic flexiList = new List<dynamic>();
 dynamic flexible = new ExpandoObject();

In my case I wanted to return the data as JSON to the Kendo Grid.
The controller ActionResult like

 var serialized = JsonConvert.SerializeObject(flexiList);
 return JSON(flexiList);

will not work as it treats the objects properties as key/value pairs which is no use.

You need to serialize it to JSON manually like

  var serialized = JsonConvert.SerializeObject(flexiList);
  return new ContentResult() { Content = serialized, ContentType = "application/json" };

That puts it in the correct format for the Kendo grid and others.


comments powered by Disqus