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

Argument Exception

1 Answer 155 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Michael
Top achievements
Rank 1
Michael asked on 18 Sep 2015, 12:00 PM

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.

1 Answer, 1 is accepted

Sort by
0
Accepted
Rosen
Telerik team
answered on 22 Sep 2015, 12:15 PM

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
Tags
Grid
Asked by
Michael
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or