Hi All,
I have a grid working without any problems when I load the data locally using BindTo(), but it takes a few seconds to load because it's a lot of data.
I wanted to play with paging on the server side to avoid the long delay, so I switched to remote binding. But now the data never gets loaded in the grid. I know the Ajax call works because I get data on the screen if I just put the URL for the Action method that produces the JSON data directly in the browser address bar.
I know it's probably something stupid that I'm not doing. Thanks in advance for any help.
Here's the code:
Contents of TestController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CONY.Models;
namespace CONY.Controllers
{
public class TestController : Controller
{
public IActionResult Index()
{
return View();
}
public JsonResult GetGridDataRemotely()
{
TestData[] data = new TestData[2];
data[0] = new TestData("Hello", "Goodbye");
data[1] = new TestData("Red", "Blue");
return Json(data);
}
public ViewResult TestGridLocalBinding()
{
TestData[] data = new TestData[2];
data[0] = new TestData("Hello", "Goodbye");
data[1] = new TestData("Red", "Blue");
ViewBag.TheData = data;
return View();
}
public ViewResult TestGridRemoteBinding()
{
return View();
}
}
}
Contents of TestGridRemoteBinding.cshtml:
@{
ViewData["Title"] = "TestGridRemoteBinding";
}
<h1>TestGridRemoteBinding</h1>
@(Html.Kendo().Grid<CONY.Models.TestData>()
.Name("TestGrid")
.Columns(columns =>
{
columns.Bound(l => l.RecordType).Width(91);
columns.Bound(l => l.GroupAcctID).Width(106);
})
.HtmlAttributes(new { style = "height: 430px;" })
.Scrollable()
.Groupable()
.Sortable()
.Resizable(resizing => resizing.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("GetGridDataRemotely", "Test").Type(HttpVerbs.Get))
)
)
<script>
$(document).ready(function () {
var grid = $("#TestGrid").data("kendoGrid");
grid.dataSource.read();
})
</script>
Contents of TestGridLocalBinding.cshtml:
@{
ViewData["Title"] = "TestGridLocalBinding";
}
<h1>TestGridLocalBinding</h1>
@(Html.Kendo().Grid<CONY.Models.TestData>()
.Name("TestGrid")
.Columns(columns =>
{
columns.Bound(l => l.RecordType).Width(91);
columns.Bound(l => l.GroupAcctID).Width(106);
})
.HtmlAttributes(new { style = "height: 430px;" })
.Scrollable()
.Groupable()
.Sortable()
.Resizable(resizing => resizing.Columns(true))
.BindTo((IEnumerable<CityOfNewYork.Models.TestData>)ViewBag.TheData)
)
Contents of TestData.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CONY.Models
{
public class TestData
{
public string RecordType { get; set; }
public string GroupAcctID { get; set; }
public TestData(string RecordType, string GroupAcctID)
{
this.RecordType = RecordType;
this.GroupAcctID = GroupAcctID;
}
}
}