ToDataSourceResult with Dictionary

4 posts, 0 answers
  1. Jeffrey
    Jeffrey avatar
    2 posts
    Member since:
    Aug 2016

    Posted 17 Aug Link to this post

    For our application, we were using Kendo Grid with a class named Properties that stored column information:

    public class Properties
    {
       
    public string Id { get; set; }
    public object PROPERTY_0 { get; set; }
    public object PROPERTY_1 { get; set; }
    ...
    public object PROPERTY_43 { get; set; }

    In order to sort and filter rows, we used the ToDataSourceResult extension. However, this system limited us to a hardcoded maximum number of columns. We have refactored the code to use a List<Dictionary<string, dynamic>> instead, which allows us to store results in key value pairs in a list thus allowing an arbitrary maximum number of columns.

    Unfortunately, I have not been able to find any documentation on whether ToDataSourceResult is able to parse data in this format, or any format aside from the previous one. While it is possible to recreate the logic for filtering, grouping, and sorting, it would take a good deal of time to do so. Is there any way to use ToDataSourceResult instead? We are storing values in a dictionary with PROPERTY_x as a string key and a dynamic value.

  2. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 19 Aug Link to this post

    Hello,

    I would suggest you to see the threads below for more information on that matter:

    https://doylestowncoder.com/2014/04/14/kendoui-understanding-todatasourceresult/
    blog.falafel.com/server-paging-sorting-filtering-kendo-datasourcerequest/

    Regards,
    Maria Ilieva
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Jeffrey
    Jeffrey avatar
    2 posts
    Member since:
    Aug 2016

    Posted 19 Aug in reply to Maria Ilieva Link to this post

    Hi, thanks for your response. I've already reviewed multiple Google results including both of these, and none of them go into actual explanation of the syntax needed to pass additional options to the ToDataSourceResult extension.

    What is happening is that grid passes a DataSourceRequest, and the FilterDescriptor contains a Member of the column name (in our system, it would be PROPERTY_2 or something similar). ToDataSourceResult seems to require a class with defined properties in order to execute its sorting logic. However, we have since converted from a class to a list of dictionaries, with each dictionary entry corresponding to a column and each item in the list corresponding to a row. ToDataSourceResult cannot seem to use the Member to filter the dictionary keys. There has been no documentation of this use case, or any examples of something similar.

    Additionally, while working on a custom solution, another issue was discovered. If we pass multiple filters in a CompositeFilterDescriptor, the MemberType is null. This only occurs with more than one filter, and is causing incorrect casting of the filter value. Is there any way to fetch the MemberType from the request object with more than one filter present?

  5. Maria Ilieva
    Admin
    Maria Ilieva avatar
    4017 posts

    Posted 23 Aug Link to this post

    Hello,

    Note that the ToDataSourceResult require a class with defined fields in order to execute sorting and filtering functionality. The MVC framework is working with models and the required functionality can not be achieved for  a list of dictionaries.
    Possible appraoch is to map the dictionary collection with Select before the ToDataSourceResult extension is applied, which is almost the same as creating a Model:
    public ActionResult EditingInline_Read([DataSourceRequest] DataSourceRequest request)
    {
        List<Dictionary<string, dynamic>> test = new System.Collections.Generic.List<System.Collections.Generic.Dictionary<string, dynamic>>();
        IEnumerable<ProductViewModel> products = productService.Read();
        foreach (var item in products)
        {
            Dictionary<string, dynamic> dict = new Dictionary<string, dynamic>();
            dict.Add("ProductID", item.ProductID);
            dict.Add("ProductName", item.ProductName);
            dict.Add("UnitPrice", item.UnitPrice);
            test.Add(dict);
        }
        var t = test.Select(item=> new ProductViewModel(){
            ProductName = item["ProductName"],
            ProductID = item["ProductID"],
            UnitPrice = item["UnitPrice"],
        }).ToDataSourceResult(request);
        return Json(t);           
    }

    I hope this helps.

    Regards,
    Maria Ilieva
    Telerik by Progress
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top
UI for ASP.NET MVC is VS 2017 Ready