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.
http://blog.codebeastie.com/kendo-grid-dynamic-columns/

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();
 flexiList.Add(flexible);

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.

References:
http://blogs.msdn.com/b/csharpfaq/archive/2009/10/01/dynamic-in-c-4-0-introducing-the-expandoobject.aspx
https://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx
http://blog.codebeastie.com/kendo-grid-dynamic-columns/


comments powered by Disqus