This is a migrated thread and some comments may be shown as answers.

[Solved] Auto-generate grid columns with collection of dynamic objects as model

1 Answer 135 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Monika
Top achievements
Rank 1
Monika asked on 13 Jun 2012, 01:59 PM

My model is an IEnumerable which I would like to bind to a telerik mvc grid. Additionally the grid should auto-generate the columns and display everything from my dynamic object.

I found several posts on the telerik forum regarding this topic, like here: http://www.telerik.com/community/forums/aspnet-mvc/general/dynamically-generate-grid-columns.aspx

Unfortunately the result is the same: the grid displays the number of total rows in the footer but no rows are shown. I attached a sample project.

Any ideas?

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 14 Jun 2012, 06:49 AM
Hello Monika,

I'm afraid that this is not supported because to the way reflection works on dynamic type. In the case in question, the IEnumerable<dynamic> is treated as IEnumerable<object> by the compiler, thus the inferred generic parameter of the Grid component will be object. This means that GetProperties on this type will yield no result. This can be tested by adding the following code to the View in the project you have sent us:

 

@model IEnumerable<dynamic>
@functions{
    int GetPropertiesCount<T>(T type)
    {
        return typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance).Length;
    }
}
 
@{     
    var propertiesCount = GetPropertiesCount(Model.First());
}
 
Count @propertiesCount @*shows the properties count*@

As you can see this restriction comes from the framework, not from the product, so I am afraid we can't be of much help here.

 Greetings,
Rosen
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the Telerik Extensions for ASP.MET MVC, subscribe to their blog feed now.
Tags
Grid
Asked by
Monika
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or