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

Is it possible to bind a collection of interfaces to Kendo Grid?

3 Answers 213 Views
Grid
This is a migrated thread and some comments may be shown as answers.
nCubed
Top achievements
Rank 1
nCubed asked on 18 Sep 2012, 06:11 PM
Using asp.net MVC 3, is it possible to bind a collection of interfaces to a Kendo Grid instead of a concrete class? For example:

Controller
IEnumerable<IFoo> items = repo.GetFoo();
return View( items );

View
@model IEnumerable<IFoo>
 
@Html.Kendo().Grid(Model).Name("Grid").Columns(column =>
{
    column.Bound(c => c.Name).Title("Foo Name");
    column.Bound(c => c.Key).Title("Foo Key");
}).DataSource(dataSource => dataSource.Ajax().ServerOperation(false))


When binding directly to IEnumerable<IFoo>, the following exception is raised:
Exception Details: System.MissingMethodException: Cannot create an instance of an interface.
 
Source Error:
Line 7:  @Html.Kendo().Grid(Model).Name("Grid").Columns(column =>

Using the Kendo Grid, are we constrained to using concrete types for everything? Or is there a way to use an interfaces?

Thanks!

3 Answers, 1 is accepted

Sort by
0
Petur Subev
Telerik team
answered on 21 Sep 2012, 12:40 PM
Hi Kindler,

Unless your Grid is editable binding to a collection of interfaces should be possible.

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!
0
phil
Top achievements
Rank 1
answered on 19 Oct 2012, 08:25 PM
Hello,

I'm having the same problem. I'm trying to bind a collection of interfaces to a grid, and am getting the same error as nCubed. The code is very similar to his, and the Grid is NOT editable. 

Here's the call stack:

[MissingMethodException: Cannot create an instance of an interface.]
   System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
   System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +113
   System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +232
   System.Activator.CreateInstance(Type type, Boolean nonPublic) +83
   System.Activator.CreateInstance(Type type) +6
   Kendo.Mvc.UI.ModelDescriptor.CreateDataItem(Type modelType) +253
   Kendo.Mvc.UI.ModelDescriptor..ctor(Type modelType) +77
   Kendo.Mvc.UI.DataSource.ModelType(Type modelType) +70
   Kendo.Mvc.UI.Grid`1..ctor(ViewContext viewContext, IJavaScriptInitializer initializer, IUrlGenerator urlGenerator, IGridHtmlBuilderFactory htmlBuilderFactory) +1509
   Kendo.Mvc.UI.Fluent.WidgetFactory.Grid() +187
0
nCubed
Top achievements
Rank 1
answered on 19 Oct 2012, 09:03 PM
Phil - we ended up changing our structure by using concrete types. One thing I didn't check, though, is verify if any of the properties on the interface are declared as interfaces themselves. If they are, then that is the likely culprit since there is no way to hydrate and rehydrate an interface between http requests.

FWIW: We are using ValueInjecter (similar, but easer than AutoMapper) to quickly map between the interface and a concrete type: http://valueinjecter.codeplex.com
Tags
Grid
Asked by
nCubed
Top achievements
Rank 1
Answers by
Petur Subev
Telerik team
phil
Top achievements
Rank 1
nCubed
Top achievements
Rank 1
Share this question
or