Set DataSource to Razor page model

1 Answer 339 Views
Grid
Mario
Top achievements
Rank 1
Mario asked on 15 Jun 2023, 01:44 AM

Hi, I want to create a shared data source that I can then apply filtering to. This data source should be initialised from a property on the Razor page model.

With a Grid you can do this:

Html.Kendo().Grid(Model.Devices)

But I can't figure out the equivalent for the DataSource. This is not allowed...

Html.Kendo().DataSource<DeviceAggregate>(Model.Devices)
                .Name("devicesDataSource")

 


1 Answer, 1 is accepted

Sort by
1
Accepted
Stoyan
Telerik team
answered on 19 Jun 2023, 04:14 PM

Hello Mario,

The DataSource in the Telerik UI for ASP.NET Core doesn't support binding to local data by itself.

To achieve the desired behavior I recommend utilizing a Kendo UI for jQuery DataSource. You can convert the List of Devices into a JavaScript array and then bind the array to the client-side DataSource:

var data = @Html.Raw(JsonSerializer.Serialize(Model.Devices));
var dataSource1 = new kendo.data.DataSource({
                        pageSize: 10,
                        data: data
});

Then bind the dataSource1 instance to the Grid:

@(Html.Kendo().Grid(Model)
    .Name("Grid")
    ...
    .DataSource("dataSource1")
)

I hope the suggested approach is useful.

Regards,
Stoyan
Progress Telerik

As of R2 2023, the default icon type will be SVG instead of Font. See this blogpost for more information.
Reza
Top achievements
Rank 1
commented on 23 Jul 2024, 03:51 PM

where should we put this code? it isn't working in document ready function:

var data = @Html.Raw(JsonSerializer.Serialize(Model.Devices));
var dataSource1 = new kendo.data.DataSource({
                        pageSize: 10,
                        data: data
});

Anton Mironov
Telerik team
commented on 26 Jul 2024, 05:58 AM

Hi Reza,

Thank you for the details provided.

Feel free to place the code in the global scope of the JavaScript.

 

Kind Regards,
Anton Mironov

Tags
Grid
Asked by
Mario
Top achievements
Rank 1
Answers by
Stoyan
Telerik team
Share this question
or