telerik-grid onRowSelect how to get id?

Sanja Melnichuk picture Sanja Melnichuk · Dec 10, 2010 · Viewed 10.6k times · Source

Hi i am new in asp.net mvc and telerik controls. How can i get o.Id value when i click on row?

 <%= Html.Telerik().Grid(Model)                    
                    .Name("RolesGrid")
                    .DataKeys(keys => keys.Add(o => o.Id))                               
                    .Selectable()                    
                    .Columns(columns =>
                    {
                        columns.Bound(o => o.Name);
                        columns.Bound(o => o.Description);

                    })
                    .Pageable()                       
                    .ClientEvents(events => events                    
                    .OnRowSelect("onRowSelect"))

             %>

in js code:

 function onRowSelect(e)   {
        var ordersGrid = $('#RolesGrid').data('tGrid');  
        var row = e.row;
        var dataItem = ordersGrid.dataItem(row);
        alert(dataItem);
    }

But dataItem is null and there is no id value in generated html file. Thanks and sorry for my bad english

Answer

Sanja Melnichuk picture Sanja Melnichuk · Jun 17, 2011

So after all i have the best way to get id is:

  1. bind onRowSelect function to your grid
  2. write next code in onRowSelect

    var dataItem = jQuery('#MyGridId').data('tGrid').dataItem(e.row);     
    alert(dataItem['Id']);
    

    dataItem is a map witch have all properties of grid model so you get all you want

Thats all, thanks