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

Avoiding Circular references causes a new problem

2 Answers 215 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 20 May 2014, 08:18 PM
Hey everyone,

I am having Entity Framework Serialization Circular reference problems when attempting to bind to my Kendo UI grid. I found the documentation about how to deal with it here:

http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/troubleshooting

Which led me to here:

http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/faq#how-do-i-avoid-circular-reference-exceptions?

I have now implemented that change on my controller and am passing an anonymous type collection to my view. However now at runtime, when I try to look at my page, it immediately throws an Exception stating

"CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type"

Below is a snippet from my grid and controller. Property3 is the one it is complaining about, which is the one I am using to bypass the object in my anonymous type collection.


The JSON is built up like the following:               

  var collection = result.ToDataSourceResult(request, c => new
                {
                       Property1 = c.Property1
                       Property2 = c.Property2
                       Property3 = c.EntityFrameworkObject.Property3
                       Property4 = c.Property4
                });               

return Json(collection, JsonRequestBehavior.AllowGet);



@(Html.Kendo().Grid<My.Namespace.To.My.EntityObject>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.Property1);
columns.Bound(p => p.Property2).Width(100);
columns.Bound(p => p.Property3).Width(100);
columns.Bound(p => p.Property4.Width(100);
columns.Command(command => { command.Edit(); }).Width(160);
})

As far as I can tell, this follows the example given. Why am I getting this "CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type" error and what do I need to do to get rid of it?

The only other thing I can think of is to remove the "My.Namespace.To.My.EntityObject, because there is no "Property3" on that object, however the compiler complains if I don't specify a type in the <> . 

I am at a loss of how to move forward here.  

2 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 22 May 2014, 02:21 PM
Hello Dave,

The problem is indeed caused because the Property3 doesn't exist. You should either create a class which has all properties:
(Html.Kendo().Grid<EntityViewModel>(


 var collection = result.ToDataSourceResult(request, c => new EntityViewModel
                {
                       Property1 = c.Property1
                       Property2 = c.Property2
                       Property3 = c.EntityFrameworkObject.Property3
                       Property4 = c.Property4 
                });                


Or specify "dynamic" and use strings in the grid column names:
@(Html.Kendo().Grid<dynamic>()
.Name("grid")
.Columns(columns =>
{
columns.Bound("Property1");
columns.Bound("Property2").Width(100);
columns.Bound("Property3").Width(100);
columns.Bound("Property4").Width(100);
columns.Command(command => { command.Edit(); }).Width(160);
})

Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Dave
Top achievements
Rank 1
answered on 22 May 2014, 09:10 PM
Thank Atanas.

I ended up creating a ViewModel object and binding the grid to that. I was confused by the example at


http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/grid/faq#how-do-i-avoid-circular-reference-exceptions?

Specifically the part below, because it just showed an anonymous type being created on the fly and passed in. As far as I can tell, the grid needs to be bound to a known type and there is no way to bind anonymous types to the grid.

    var result = orders.ToDataSourceResult(request, o => new {
        OrderID = o.OrderID,
        CustomerName = o.Customer.ContactName
    });








Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Dave
Top achievements
Rank 1
Share this question
or