Kendo MVC Grid - 获取单元格值

时间:2016-02-23 21:52:14

标签: c# asp.net-mvc telerik kendo-asp.net-mvc

我正在将我的应用程序表单MVC Extension迁移到用于ASP.NET MVC的Telerik UI。将网格更改为:

@(Html.Kendo().Grid((IEnumerable<vw_Client_info>)ViewBag.Clients)
 .Name("Clients")
 .Columns(columns =>
     {
      columns.Bound(c => c.ClientID).Width(30).Title("Client ID").Hidden();
      columns.Bound(c => c.FullName).Width(130);
     })
  .DataSource(dataSource => dataSource
  .Ajax()
  .Model(model => model.Id(p => p.ClientID))
  .Read(read => read.Action("AllClientsSelection", "Visit")))
  .Selectable()
  .Events(events =>
    {  events.Change("onRowSelected"); })
 .RowAction(row => row.Selected = row.DataItem.ClientID.Equals(ViewData["id"]))
                )

要获取单元格值,我之前使用过:

function onRowSelected(e) {
        var detailGrid = $('#Visit').data('tGrid');
        id = e.row.cells[0].innerHTML;
        fullname = e.row.cells[1].innerHTML;
        $('#ClientFullName').text(fullname);
          detailGrid.rebind();
    }

这适用于Telerik MVC Extensions,但不适用于新版本,我收到此错误:

Unable to get property 'cells' of undefined or null reference.

我试图使用:

 function onRowSelected(e) {
        var detailGrid = $('#Visit').data('kendoGrid');
        var id = e.row.ClientID;
        var fullname = e.row.FullName;
        $('#ClientFullName').text(fullname);
          detailGrid.rebind();
    }

我收到此错误:

Unable to get property 'ClientID' of undefined or null reference

2 个答案:

答案 0 :(得分:2)

我找到了答案:

 var grid = $("#Clients").data("kendoGrid");
        var selectedItem = grid.dataItem(grid.select());
        id = selectedItem.ClientID;
        fullname = selectedItem.FullName;

答案 1 :(得分:0)

这是因为'row'的值在:

var id = e.row.ClientID;

为空。因此,您可能希望在将ClientID或cells [0]或cells [1]设置为变量之前检查行是否为null。所以也许:

var id = null;
if(e.row != null)
   id = e.row.ClientId

但是,我认为你可能需要调试并找出你是否正在获得任何'行'计数。因为如果没有'row',那么就没有'row.ClientId'。