I am having an issue which I am hoping is obvious but that I haven't been able to figure out.
I have a grid that when I use call the Read controller the MVC grid it works perfectly fine as below.
@(Html.Kendo().Grid<
ProductModel
>()
.Name("grid1")
.Columns(columns =>
{
columns.Bound(p => p.Id).Filterable(false);
columns.Bound(p => p.Name);
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.HtmlAttributes(new { style = "height:550px;" })
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Read(read => read.Action("ProductRead", "Admin"))
)
)
But when I use Jquery to create the grid dynamically it makes the call to the controller and retrieves the same JSON correctly as below, but the grid is always blank. The databound event also
{"Data":[{"Id":1,"Name":"Testing1"},{"Id":2,"Name":"Testing2"},{"Id":3,"Name":"Testing3"},],"Total":3,"AggregateResults":null,"Errors":null}
Here is the javascript. I must be missing something:
<script>
CreateGrid(
"myGrid"
);
function
CreateGrid(gridID) {
var
dataSource =
new
kendo.data.DataSource({
//type: "odata",
transport: {
read:
function
(options) {
$.ajax({
url:
"/Admin/Wells/ReadAll"
,
dataType:
"json"
,
data: {
models: kendo.stringify(options.data.models)
},
success:
function
(result) {
options.success(result);
}
});
},
parameterMap:
function
(options, operation) {
if
(operation !==
"read"
&& options.models) {
return
{
models: kendo.stringify(options.models)
};
}
}
},
batch:
true
,
pageable:
true
,
schema: {
model: {
id:
"Id"
,
fields: {
"Id"
: {
"type"
:
"number"
,
"nullable"
:
false
},
"Name"
: {
"type"
:
"string"
,
"nullable"
:
false
}
}
}
}
});
$(gridID).kendoGrid({
sortable:
true
,
autoBind:
false
,
dataSource: dataSource,
filterable:
true
,
columnMenu:
true
,
resizable:
true
,
selectable:
"single"
,
autoFitColumn:
false
,
pageable: {
pageSize: 10
// specifying the pagesize inside the datasource fixed my problem (Nan-Nan of 1 items)
},
//columns: myColumnObject,
columns: [{ field:
"Id"
, minResizableWidth:
"125px"
, type:
"number"
},
{ field:
"Name"
, minResizableWidth:
"350px"
}
],
editable:
"popup"
});
//Now load the grid with data
var
grid = $(gridID).data(
"kendoGrid"
);
grid.bind(
"dataBound"
, grid_dataBound);
grid.dataSource.fetch();
}
function
grid_dataBound(e) {
console.log(
"dataBound"
);
}
</script>
and for reference here is the controller it hits.
Productcontroller.cs
public
ActionResult ReadAll([DataSourceRequest] DataSourceRequest request)
{
var data = productService.Read();
var serializer =
new
JavaScriptSerializer();
var result =
new
ContentResult();
serializer.MaxJsonLength = Int32.MaxValue;
// Whatever max length you want here
result.Content = serializer.Serialize(data.ToDataSourceResult(request));
result.ContentType =
"application/json"
;
return
result;
}
productService.cs
public IEnumerable<
ProductModel
> Read()
{
return GetAll();
}
public IQueryable<ProductModel> GetAll()
{
var wells = entities.WellModel.AsNoTracking().AsQueryable();
return wells;
}
Sorry I copy and pasted the wrong productservice.cs and I can't edit my post. That last ProduceService.cs is actually this:
public
IEnumerable<ProductModel> Read()
{
return
GetAll();
}
public
IQueryable<ProductModel> GetAll()
{
var products = entities.ProductModel.AsNoTracking().AsQueryable();
return
products;
}