2

I have a ASP.Net MVC Kendo Grid with checkbox selection, as the documentation I added the checkbox using columns.Select().Width(50); as:

@model Project.Lib.ViewModels.Campaigns.CampaignViewModel

  @(Html.Kendo().Grid(Model.Stations)
                                .Name("grid")
                                .DataSource(dataSource => dataSource
                                    .Custom()
                                    .PageSize(10)
                                )
                                .PersistSelection(true)
                                .Sortable()
                                .Events(ev => ev.Change("onChange"))
                                .Columns(columns =>
                                {
                                    columns.Select().Width(50);
                                    columns.Bound(x => x.StationId)
                                        .Hidden();
                                   ...
                                })
                                )

Then I use javascript onChange event as example to try to retrieve selected checkboxes as:

function onChange(arg) {
                 var grid = $("#grid").data("kendoGrid");
                    console.log("The selected product ids are: [" + grid.selectedKeyNames().join(", ") + "]");
                }

So when I debug in console, the grid.selectedKeyNames value is always empty, so when I check a box it always show log as:

The selected product ids are: []

So I have two questions here:

Why the value is always empty?

How can I bind the selected id row to be my stationId column value and finally send them to the controller post-action through the ViewModel?

Regards

2
  • Can you create a reproducible snippet? Commented Nov 27, 2022 at 17:02
  • Which Kendo version are you using> Commented Nov 28, 2022 at 18:40

1 Answer 1

3
+50

Do you have your model id defined in the DataSource? From the Telerik code example - I've put an arrow pointing to the portion I'm referring to below:

@(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
.Name("grid")
.Columns(columns =>
{
    columns.Select().Width(50);
    columns.Bound(p => p.ProductName);
    ...
})
...
.Events(ev => ev.Change("onChange"))
.DataSource(dataSource => dataSource
    .Ajax()
    .Model(model => model.Id(p => p.ProductID)) <-------------- do you have this?
    .Read(read => read.Action("Selection_Read", "Grid"))
)

)

Sign up to request clarification or add additional context in comments.

1 Comment

Setting the model id is indeed a prerequisite for the selectedKeyNames method to work as expected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.