1
votes

In a MVC4 project, I've got a kendo grid that fires an event when a row is selected.

<div id="datagrid">
@(Html.Kendo().Grid<SustIMS.Models.StretchModel>()
    .Name("datagrid_Stretches")
    .Columns(columns =>
    {
        columns.Bound(s => s.StretchCode).Title(ViewBag.lblCode).Width(80);
        columns.Bound(s => s.StretchMediumDescription).Title(ViewBag.lblDescription);
        ...
    })
    .Events(e => e.Change("onChange"))    <--------- here's the event
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(15)
        .Read(read => read.Action("GetStretches", "MasterData"))
    )
)
</div>

So, when I select a row, the onChange function is called:

function onChange() {
    var code = this.dataItem(this.select()).StretchCode;
    $.get('@Url.Content("getStretchCode")',
        { "code": code });
}

In my controller, the code is retrieved and some operations are done with it.

public void getStretchCode(string code)
{
    currentStretch.RoadComponentCode = code;
    ...
}

This works fine. The problem is, the event is fired every time I select a different row but if I select a row that was previously selected, the event isn't fired and I can't get the code of that row.

Any help?

2
Hi, can you please provide more details may be a sample jsbin of your code, because it fires each time. Please have a look at this link: dojo.telerik.com/@NitinMall/Axap - Nitin Mall
I don't know, as you can see the grid is Razor HTML helper code. The grid doesn't show in jsbin - chiapa
The event fires when the row selection changes. Selecting the same row that's already selected changes nothing, so no event is fired. - Brett
@Brett, you must have misunderstood. Picture this: I select row1 -> the event fires and gives me id=1; I select row2 -> the event fires and gives me id=2; I select row1 again -> the event isn't fired and the id remains id=2. It's not "selecting the same row that's already selected", as you said, but a row that has been selected before. Can you help? - chiapa
Did you forget to define the grid as .Selectable()? - Brett

2 Answers

2
votes

Add .Selectable() in your grid so it allow you select previously row.

@(Html.Kendo().Grid<SustIMS.Models.StretchModel>()
    .Name("datagrid_Stretches")
    .Columns(columns =>
    {
        columns.Bound(s => s.StretchCode).Title(ViewBag.lblCode).Width(80);
        columns.Bound(s => s.StretchMediumDescription).Title(ViewBag.lblDescription);
        ...
    })
 .Selectable(selectable => selectable
            .Type(GridSelectionType.Row)  <--------- Add This
            )
    .Events(e => e.Change("onChange"))    
    .DataSource(dataSource => dataSource
        .Ajax()
        .PageSize(15)
        .Read(read => read.Action("GetStretches", "MasterData"))
    )
)

Script

function onChange() {
    var code = this.dataItem(this.select()).StretchCode;
    $.post('@Url.Content("getStretchCode")',
        { "code": code });
}
2
votes

you should add Selectable() before add event like this

 .Selectable(selectable => selectable
            .Mode(GridSelectionMode.Multiple)
            .Type(GridSelectionType.Cell))
.Events(e => e.Change("onChange"))