Hi
I'm still trying to find my way with Telerik UI for ASP.NET MVC, I'm not sure what am I doing wrong here but my grid is not displaying any data here is my code....
[AcceptVerbs(
"Get"
)]
public
ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
{
List<OrderViewModel> OrderList =
new
List<OrderViewModel>();
OrderList.Add(
new
OrderViewModel()
{
Freight =2124,
OrderID = 1,
ShipCity =
"test"
,
ShipName =
"Test"
});
var dsResult = OrderList.ToDataSourceResult(request);
return
Json(dsResult);
}
@(Html.Kendo().Grid<
AdorReactUI.Models.OrderViewModel
>()
.Name("grid")
.AutoBind(false)
.Columns(columns =>
{
columns.Bound(p => p.OrderID).Filterable(false);
columns.Bound(p => p.Freight);
columns.Bound(p => p.ShipName);
columns.Bound(p => p.ShipCity);
columns.Command(c => c.Edit());
})
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.ToolBar(toolbar =>
{
toolbar.Create();
})
.Editable(editable => editable.Mode(GridEditMode.InLine))
.HtmlAttributes(new { style = "height:550px;" })
.DataSource(dataSource => dataSource
.Custom()
.Schema(schema =>
{
schema.Model(m =>
{
m.Id(field => field.OrderID);
m.Field(field => field.OrderID).Editable(false);
});
// schema.Data("Data");
})
.Transport(transport =>
{
transport.Read(read => read.Url("/Grid/Orders_Read").Type(HttpVerbs.Get));
transport.Create(read => read.Url("/Grid/EditingInline_Create").Type(HttpVerbs.Post));
transport.Update(read => read.Url("/Grid/EditingInline_Update").Type(HttpVerbs.Post));
transport.Destroy(read => read.Url("/Grid/EditingInline_Destroy").Type(HttpVerbs.Post));
})
.PageSize(20)
)
)
I have investigated the provided configuration and noticed few things. The grid is configured to not bind automatically.
e.g.
@(Html.Kendo().Grid<AdorReactUI.Models.OrderViewModel>()
.Name(
"grid"
)
.AutoBind(
false
)
Furthermore, a custom DataSource is used, but a DataSourceResult object is returned from the server. If you change the type to AJAX you can omit manually configuring which property of the response object contains the array of data, the aggregates, etc...
e.g.
.DataSource(dataSource => dataSource
.Ajax()
.Model(m =>
{
m.Id(field => field.OrderID);
m.Field(field => field.OrderID).Editable(
false
);
})
.Read(read => read.Url(
"/Grid/Orders_Read"
).Type(HttpVerbs.Get))
.Create(read => read.Url(
"/Grid/EditingInline_Create"
).Type(HttpVerbs.Post))
.Update(read => read.Url(
"/Grid/EditingInline_Update"
).Type(HttpVerbs.Post))
.Destroy(read => read.Url(
"/Grid/EditingInline_Destroy"
).Type(HttpVerbs.Post))
.PageSize(20)
)
Finally, a GET request is sent for fetching the data. By default the MVC framework block get requests. It is necessary to set the JsonRequestBehavior.AllowGet in order to allow GET requests.
e.g.
[AcceptVerbs(
"Get"
)]
public
ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
{
List<OrderViewModel> OrderList =
new
List<OrderViewModel>();
OrderList.Add(
new
OrderViewModel()
{
Freight = 2124,
OrderID = 1,
ShipCity =
"test"
,
ShipName =
"Test"
});
var dsResult = OrderList.ToDataSourceResult(request);
return
Json(dsResult, JsonRequestBehavior.AllowGet);
}
For your convenience I am attaching a small sample which I created using the provided configuration.
Regards,
Georgi
Progress Telerik
Hi Georgi
Thanks for the reply , the reason i set .AutoBind(false) is because of the issues i had which was resolved in this post, and i can't come right with JsonRequestBehavior.AllowGet beacuse I'm using .Net core .. i have refine my code :
@(Html.Kendo().Grid<
Models.OrderViewModel
>()
.Name("grid")
.Columns(columns =>
{
columns.Bound(p => p.OrderID).Filterable(false);
columns.Bound(p => p.Freight);
columns.Bound(p => p.ShipName);
columns.Bound(p => p.ShipCity);
columns.Command(c => c.Edit());
})
.ToolBar(toolbar =>
{
toolbar.Create();
toolbar.Save();
})
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.Pageable()
.AutoBind(false)
.Scrollable(scr => scr.Height(800))
.Reorderable(reorderable => reorderable.Columns(true))
.DataSource(dataSource => dataSource
.Custom()
.PageSize(20)
.Schema(schema =>
{
schema.Model(m =>
{
m.Id(field => field.OrderID);
m.Field(field => field.OrderID).Editable(false);
});
// schema.Data("Data");
})
.Transport(transport =>
{
transport.Read(read =>
read.Url( "/api/Grid/Orders_Read")
.DataType("json")
.Type(HttpVerbs.Get)
);
transport.Create(create =>
create.Url( "/api/Grid/EditingInline_Create")
.DataType("json")
.Type(HttpVerbs.Post)
.ContentType("application/json; charset=utf-8")
);
transport.Update(update =>
update.Url( "/api/Grid/EditingInline_Update")
.DataType("json")
.Type(HttpVerbs.Put)
.ContentType("application/json; charset=utf-8")
);
transport.Destroy(destroy =>
destroy.Url( "/api/Grid/EditingInline_Destroy")
.DataType("json")
.Type(HttpVerbs.Delete)
);
})
)
)
I got confused as the thread is in the MVC section.
However, a common cause for the described behavior in ASP.Net Core is the serialization of the data. By default, the data in .NET Core is serialized in camelCase, while the dataSource expectes the model to be serialized in PascalCase. As a result, in this scenario the DataSource of the Grid does not recognize the fields in the data that is returned by the server.
Point five of our Getting Started with Telerik UI for ASP.Net Core illustrates how to modify the serialization of the objects in order to be compatible with the DataSource:
Please modify the JSON serialization as shown in the article above and let me know if the data is displayed as expected.
Regards,
Georgi
Progress Telerik
hey Georgi
i have modify the JSON serialization as shown in the article, but still the data doesn't show , its only show if ToDataSourceResult(request)
[AcceptVerbs("Get")]
public ActionResult Orders_Read([DataSourceRequest]DataSourceRequest request)
{
List<
OrderViewModel
> OrderList = new List<
OrderViewModel
>();
OrderList.Add(new OrderViewModel()
{
Freight = 2124,
OrderID = 1,
ShipCity = "test",
ShipName = "Test"
});
//var dsResult = OrderList.ToDataSourceResult(request);
return Json(OrderList);
}
how can i make it work with it?