Argument Exception

2 posts, 1 answers
  1. Michael
    Michael avatar
    2 posts
    Member since:
    Jul 2015

    Posted 18 Sep 2015 Link to this post

    Hi all,

    I am trying to display an object in a Kendo grid in a MVC Website, however this object contains a dictionary and it seems like the grid is unable serialize it.

    This is the error message I am getting:

    Type 'System.Collections.Generic.Dictionary`2[[ExactModels.SalesOrderLine, ExactModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[ExactModels.Item, ExactModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not supported for serialization/deserialization of a dictionary, keys must be strings or objects.

    I am using Kendo.Mvc.dll version 2015.2.902.545.

    And this is the object which contains the dictionary that is causing the problem.

    public class CustomOrder
    {
        [Key]
        public Guid ID { get; set; }
        //public SalesOrder salesOrder { get; set; }
     
        private SalesOrder order;
         
        public SalesOrder salesOrder
        {
            get { return order; }
            set
            {
                order = value;
                if (order != null)
                {
                    this.ID = order.OrderID;
                }
            }
        }
     
        public Account account { get; set; }
        public Dictionary<SalesOrderLine, Item> salesOrderLineItem { get; set; }
        public Boolean Checked { get; set; }
    }

    Anyone any idea of how to fix this?

    Thanks in advance.

  2. Answer
    Rosen
    Admin
    Rosen avatar
    3253 posts

    Posted 22 Sep 2015 Link to this post

    Hello Michael,

    As you may know the Grid does not serialize the data on its own but uses the built-in ASP.NET MVC serializer.  In case of the AJAX binding the ToDataSourceResult result is passed into a JsonResult which is responsible for the data serialization into JSON. Having this in mind, the issue you are experiencing is limitation of JavaScriptSerializer which as the exception suggest, cannot handle the Dictionary<SalesOrderLine, Item> serialization. You could observe the same error if you try the following:

    return Json(new CustomOrder()
    {
        salesOrderLineItem = new Dictionary<SalesOrderLine, Item> {
            { new SalesOrderLine(), new Item() }
        }
    });

    In order this to work you should change the dictionary keys to be strings instead.

    Regards,
    Rosen
    Telerik
    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