Kendo MVC Grid - data binding to dynamic object model

19 posts, 0 answers
  1. Gunawan Thio
    Gunawan Thio avatar
    3 posts
    Member since:
    Apr 2010

    Posted 27 Jul 2012 Link to this post


    Hi there, is there a way to bind Kendo MVC grid to dynamic object (or ExpandoObject)?
  2. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 01 Aug 2012 Link to this post

    Hi Gunawan,

    I created a code-library project which you could download and try here.

    I hope this helps.

    Kind Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Rick
    Rick avatar
    20 posts
    Member since:
    Aug 2012

    Posted 07 Aug 2012 Link to this post

    Petur,

        This is an interesting example. I have been working with it to see if I can get the grid wroking with my dynamic objects.

        Unfortunately, unless I'm missing something, your example is really just casting a list of non dynamic objects to a list of dynamic objects. The underlying type remains non-dynamic and the example works. However, if I create actual dynamic objects and try to get them to the grid via ajax, the data does not display.

        For example, if you build the list like this:
    var dataList = new List<System.Dynamic.ExpandoObject>();
    dynamic row = new System.Dynamic.ExpandoObject();
    row.StringValue = "FirstString";
    row.IntValue = 1;
    dataList.Add(row);
    row = new System.Dynamic.ExpandoObject();
    row.StringValue = "SecondString";
    row.IntValue = 2;
    dataList.Add(row);
    ...

       This gies you a list of dynamic objects, but the grid does not display the values even though it does recognize that there are rows in the datasource.

        I have successfully displayed dynamic data like this on the KendoUI Grid, but only by passing it in as the model data. The part that doesn't work (as far as I can tell) is when I try to use Ajax, which involves serializing and deserializing the data. I realize that the ExpandoObject is not serializable, so in my project I created a serializable dynamic object and tried to use that, but it didn't solve the problem. the data still does not display. It seems that the json data is not deserialized in a way that the grid can use it, even if I specify the fields of the model using .Model().

       I need to use a datasource with Ajax because some of the grids that I will display could have thousands of lines, which will cause a performance problem if is all loaded into the browser.

       Rick.
  5. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 11 Aug 2012 Link to this post

    Hi Rick,

    The issue with the ExpandoObject comes when they are being serialized into Json, because it is serialized as KeyValuePairs which is unreadable by the Grid. 
    I tried to change the way the dataSource is serialized and it seems it is working fine. However the ToDataSourceResult is not capable of applying filtering over the collection of ExpandoObjects - I am not sure if this could be achieved with CustomBinding.
    Please check the attached project and use it as a base point to extend it with custom binding. 
    I hope this helps.

    Kind Regards,
    Petur Subev
    the Telerik team
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  6. Chris Williams
    Chris Williams avatar
    63 posts
    Member since:
    Oct 2004

    Posted 16 Aug 2012 Link to this post

    I've gotten binding working against a List of ExpandoObjects using server binding.  I've attached an example below.

    What I'm having trouble with is sorting against the model.  I see that the GET request comes in with a request parameter like "DataGrid-sort", but I'd like to handle the sorting on the server and return the data without manipulation by Kendo.  I'm currently getting an error that I'm trying to trace down, but it has to do with not being able to find the property on my ExpandoObject. Any ideas appreciated.

    Here's my definition:

    @(Html.Kendo().Grid(Model.Results)
    .Name("DataGrid")           
    .Columns(column =>
            {
                foreach (var dataSeries in Model.Report.DataSeriesCollection)
                {
                    DataSeries series = dataSeries;
                    column
                        .Bound(string.Concat(series.Source, "-", series.Field))
                        .Encoded(false)
                        .Template(t =>
                                      {
                                          IDictionary<String, Object> values = (IDictionary<String, Object>)t;
                                          object value;
                                          values.TryGetValue(string.Concat(series.Source, "-", series.Field), out value);
                                          return Html.WriteValue(values, value, series); //custom formatting
                                      }
                        )
                        .HeaderTemplate(@<text><strong>@series.DisplayName</strong></text>)
                        .Hidden(series.Hidden)                           
                        .Title(series.DisplayName)
                        .Sortable(true)
                        ;
                }                                                               
            })
            .Reorderable(reorder => reorder.Columns(true))
            .Sortable(x => x.Enabled(true)) 
    )
  7. comgiu
    comgiu avatar
    29 posts
    Member since:
    Jun 2011

    Posted 20 Aug 2012 Link to this post

    Hi
    can you post e zipped example?

    thanks
  8. Tom
    Tom avatar
    1 posts
    Member since:
    Jan 2013

    Posted 06 Feb 2013 Link to this post

    Hi Petur (Subev),
    I tired your zipped example and it works fine with a editable view template - you use the line:

    .Editable(editable => editable.TemplateName("ProductItem").Mode(GridEditMode.PopUp))

    Is it possible, however, to avoid the template and use:

    .Editable(editable => editable.Mode(GridEditMode.InCell))

    ...and possibly:

    .ToolBar(toolBar => toolBar.Save())

    Whenever I try I get an Invalid Op exception: 

    "Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."

    Is there a way round this? If so, then my data definition at all levels can be truly dynamic!  (Or do I have to create a VirtualPathProvider to send a dynamically created ProductItem.cshtml through?)

    Thanks,
    Tom.


  9. Bram
    Bram avatar
    8 posts
    Member since:
    May 2012

    Posted 09 Apr 2013 Link to this post

    Any answer on this post Tom ?
    How do you solved it ?

    Paul
  10. Peter
    Peter avatar
    22 posts
    Member since:
    Nov 2007

    Posted 19 Nov 2013 Link to this post

    Has anyone made any progress on this or come up with a new solution?  I've been able to get my data to show using the IEnumerable<dynamic> , however, I really need to be able to use the inline editing.  
  11. David
    David avatar
    3 posts
    Member since:
    May 2013

    Posted 09 Apr 2014 Link to this post

    Hello

    I have managed to get the grid working with <dynamic> data type.

    So far without editing it has been working great.

    I am also trying to use a heirarchy grid.

    As soon as I try and expand my heirarchy it complains regarding the primary key field of the level up row that I am trying to expand.

    Any help most appreciated.

    Kind Regards

    David.
  12. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 14 Apr 2014 Link to this post

    Hello David,

    Could you please provide us a small project which we can run and see what exactly goes wrong? Feel free to modify my project and send it back.

    Regards,
    Petur Subev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  13. Aarti
    Aarti  avatar
    20 posts
    Member since:
    Mar 2014

    Posted 16 Apr 2014 Link to this post

    Hi

    Can someone please provide me an example for ajax custom binding of kendo grid to dymanic resultset of stored procedure. Scenario is stored procedure returns 2 resultsets - first is schema and second is actual data. But I am not sure how I can bind the data using the schema returned by stored procedure as schema provides the information ablut columns ,their data types etc..
  14. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 21 Apr 2014 Link to this post

    Hello Aarti,

    I am afraid this is not possible, you will have to create new dataSource each time based on the schema that you got from the stored procedure call. I would suggest requesting the data with regular $.ajax request and then inside the success callback dynamically create new dataSource with the appropriate schema setting, also you might need to specify the read transport operation as function.

    Kind Regards,
    Petur Subev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  15. hkdave95
    hkdave95 avatar
    245 posts
    Member since:
    Nov 2006

    Posted 21 Apr 2014 in reply to Petur Subev Link to this post

    Hi All

    Petur - I think I have a solution using DataSets.

    I will try and create a code example and post in the last two weeks of May when I will have some time.

    Kind Regards

    David
  16. hkdave95
    hkdave95 avatar
    245 posts
    Member since:
    Nov 2006

    Posted 21 Apr 2014 in reply to hkdave95 Link to this post

    Hi All

    Update  - The solution is for a grid with columns of a dynamic nature using ASP.NET MVC methodology and C#.

    Just in case you will waiting with baited breath for something more jquery in nature.

    Thank you

    David
  17. Petur Subev
    Admin
    Petur Subev avatar
    1882 posts

    Posted 24 Apr 2014 Link to this post

    Hello Gunawan,

    Feel free to submit your sample as a code library project which the community can make use of.

    http://www.telerik.com/support/code-library/aspnet-mvc/grid

    Kind Regards,
    Petur Subev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  18. hkdave95
    hkdave95 avatar
    245 posts
    Member since:
    Nov 2006

    Posted 18 Jun 2014 Link to this post

    Hi All

    Sorry, I had a brilliant holiday in May and did not get around to posting a code sample.

    This shall have to wait as the boss has me working fairly flat out on loads of issues that keep cropping up.

    David
  19. Vince
    Vince avatar
    7 posts
    Member since:
    Apr 2013

    Posted 26 Feb 2015 Link to this post

    Hi David were u ever able to get a solution to this? If u could post a sample it would be much appreciated?

    Vince.
  20. hkdave95
    hkdave95 avatar
    245 posts
    Member since:
    Nov 2006

    Posted 15 Jul in reply to Vince Link to this post

    Hello Vince

    I got it working. However, I had to modify the Kendo.Mvc.dll.

    If you really want me to share the result with you email me at david@dar-jader.com

    I have not got enough time to put the solution into a code sample and package for Telerik.

    David

Back to Top
UI for ASP.NET MVC is VS 2017 Ready