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

How to bind Grid to Web API Controller?

3 Answers 390 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Dave
Top achievements
Rank 1
Dave asked on 20 Jul 2012, 08:10 PM

I'm having trouble binding data from a Web API Controller to a Grid. Unfortunately I haven't been able to find any examples of this.

Here's the API Controller:

public class FruitController : ApiController
{
    public class Fruit
    {
        public string Name { get; set; }
        public string Color { get; set; }
    }
 
    public IEnumerable<Fruit> GetFruits()
    {
        List<Fruit> list = new List<Fruit>();
 
        Fruit f = new Fruit();
        f.Name = "Apple";
        f.Color = "Red";
 
        list.Add(f);
 
        f = new Fruit();
        f.Name = "Kiwi";
        f.Color = "Green";
 
        list.Add(f);
 
        return list;
    }
}


And in my .cshtml file I have:

@model IEnumerable<FruitController.Fruit>
 
    @(Html.Kendo().Grid(Model)   
        .Name("Grid")
        .Columns(columns =>
        {
            columns.Bound(p => p.Name);
            columns.Bound(p => p.Color);
        })
        .Groupable()
        .Pageable()
        .Sortable()
        .Scrollable()
        .Filterable()
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("GetFruits", "api/Fruit").Type(HttpVerbs.Get)
 
            )
        )
    )



When I run this, I get a successful JSON response from the controller (seen via Chrome):

[{"Name":"Apple","Color":"Red"},{"Name":"Kiwi","Color":"Green"}]

But the grid has no data in it. Is there something obvious I am missing? I haven't been able to figure this out!

Thanks!

3 Answers, 1 is accepted

Sort by
0
Oscar
Top achievements
Rank 1
answered on 06 Aug 2012, 04:11 AM
Hello Telerik team.
Same question as Dave, here
Regards.
0
Calvin
Top achievements
Rank 2
answered on 07 Sep 2012, 03:09 PM
I'd also like to know how to do this.  thanks
0
Tyler
Top achievements
Rank 1
answered on 07 Sep 2012, 09:20 PM
It sounds like the ApiControllers dont work with Kendo MVC right now.

See the second post in this thread
http://www.kendoui.com/forums/mvc/grid/kendo-mvc-grid-webapi-and-odata.aspx
Tags
Grid
Asked by
Dave
Top achievements
Rank 1
Answers by
Oscar
Top achievements
Rank 1
Calvin
Top achievements
Rank 2
Tyler
Top achievements
Rank 1
Share this question
or