chiapa
chiapa

Reputation: 4412

Kendo grid doesn't show any data

I'm trying to use a Kendo grid for the first time: Here's the code for the grid:

@model IEnumerable<SustIMS.Models.ModelTest>

<div id="grid">
    @(Html.Kendo().Grid(Model)
        .Name("datagrid")
        .Columns(columns =>
        {
            columns.Bound(c => c.Genre).Width(140);
            columns.Bound(c => c.Title).Width(190);
            columns.Bound(c => c.ReleaseDate);
            columns.Bound(c => c.Price).Width(110);
        })
        .HtmlAttributes(new { style = "height: 90%;" })
        .Scrollable()
        .Sortable()
        .Pageable(pageable => pageable
            .Refresh(true)
            .PageSizes(true)
            .ButtonCount(5))
        .DataSource(dataSource => dataSource
            .Ajax()
            .Read(read => read.Action("Show", "MasterData"))
        )
    )
</div>

This displays the grid without any data in it. I've created a test model and am trying to add data like this in the controller:

public ActionResult Show()
{
    var list = new List<ModelTest>();
    list.Add(new ModelTest { Title = "A Title", Price = 10.5m, ReleaseDate = DateTime.Now, ID = 1, Genre = "zz" });
    list.Add(new ModelTest { Title = "B Title", Price = 11.5m, ReleaseDate = DateTime.Now, ID = 2, Genre = "zzzzzz" });

    return Json(list);
}

This doesn't show any data in the grid. How can I pass data into it? And does it have to come from a model or can it be added in a custom way?

Upvotes: 2

Views: 1629

Answers (1)

Kemal Duran
Kemal Duran

Reputation: 1522

try this:

public ActionResult Show([DataSourceRequest] DataSourceRequest request)
    {
        var list = new List<ModelTest>();
        list.Add(new ModelTest { Title = "A Title", Price = 10.5m, ReleaseDate = DateTime.Now, ID = 1, Genre = "zz" });
        list.Add(new ModelTest { Title = "B Title", Price = 11.5m, ReleaseDate = DateTime.Now, ID = 2, Genre = "zzzzzz" });

        return Json(list.ToDataSourceResult(request));
    }

then put this in your controller:

using Kendo.Mvc.Extensions;

Upvotes: 4

Related Questions