Hi,
I am trying to create HTML5 Kendo UI Grid which will receive JSON data from ASP.NET WebApi.
Below is this ApiController which returns JSON as below-
public class ProductsController : ApiController
{
// GET api/<controller>
public JsonResult Get()
{
var jsonString = "[{\"ProductID\":1,
\"ProductName\":\"Chai\", \"UnitPrice\":18, \"UnitsInStock\":39,
\"Discontinued\":false},{\"ProductID\":2, \"ProductName\":\"Coffee\",
\"UnitPrice\":18, \"UnitsInStock\":39, \"Discontinued\":false}]";
var result = new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = JsonConvert.SerializeObject(jsonString),
};
return result;
}
Below is the HTML part where Kendo Grid read data from WebApi Get method
angular.module("KendoDemos", ["kendo.directives"])
.controller("MyCtrl", function ($scope) {
$scope.mainGridOptions = {
dataSource: {
transport: {
read: {
url: "http://localhost:50986/api/Products", //url: "api/Products",
type: "GET",
dataType: "json"
},
},
pageSize: 25,
serverPaging: true,
serverSorting: true,
batch: true,
schema: {
data: "d",
type: 'json',
model: {
id: "ProductID",
fields: {
ProductID: { editable: false, nullable: false },
ProductName: { validation: { required: true } },
UnitPrice: { type: "number", validation: { required: true, min: 1 } },
Discontinued: { type: "boolean" },
UnitsInStock: { type: "number", validation: { min: 0, required: true } }
}
}
}
},
navigatable: true,
height: 700,
pageable: true,
filterable: true,
//columnMenu: true,
selectable: true,
toolbar: ["create", "save", "cancel", "excel"],
columns: [
{ field: "ProductName", title: "Product Name" },
{ field: "UnitPrice", title: "Unit Price", format: "{0:c}", width: 120, filterable: { multi: true } },
{ field: "UnitsInStock", title: "Units In Stock", width: 120, filterable: { multi: true } },
{ field: "Discontinued", width: 120, filterable: { multi: true } },
{ command: ["edit", "destroy"], title: " ", width: 350 }],
editable: "popup"
};
})
Whenever I ran this example it doesn't display any data on screen. Could you please help me on this?
Cheers
Sanket