我在我的ASP.NET MVC应用程序可选kendoUI网格。 如何获得所选项目的强类型的模型对象? 我曾尝试下面的代码,但它不工作。
@(Html.Kendo().Grid<Backup>(Model.Backups)
.Name("MatchingBackupsGrid")
.Columns(col =>
{
col.Bound(backup => backup.BackupUId).Title("UID");
col.Bound(backup => backup.BackupFirstName).Title("First Name");
col.Bound(backup => backup.BackupLastName).Title("Last Name");
})
.Scrollable()
.Selectable(sel =>
{
sel.Mode(GridSelectionMode.Single);
sel.Type(GridSelectionType.Row);
})
.DataSource(dataSource => dataSource
.Server()
.Read(read => read.Action("SearchForBackup", "Arr", new { lastName = Model.SearchTerm }))
.Model(model => model.Id(backup => backup.BackupUId))
)
)
function SelectBackupButtonClickHandler() {
var grid = $("#MatchingBackupsGrid").data("kendoGrid");
var selectedBackup = grid.dataItem(grid.select());
console.log(selectedBackup);
}
网格行选择响应剑道change
事件。
@(Html.Kendo().Grid<Backup>(Model.Backups)
.Name("MatchingBackupsGrid")
.Columns(col =>
{
col.Bound(backup => backup.BackupUId).Title("UID");
col.Bound(backup => backup.BackupFirstName).Title("First Name");
col.Bound(backup => backup.BackupLastName).Title("Last Name");
})
.Scrollable()
.Selectable(sel =>
{
sel.Mode(GridSelectionMode.Single);
sel.Type(GridSelectionType.Row);
})
.DataSource(dataSource => dataSource
.Server()
.Read(read => read.Action("SearchForBackup", "Arr", new { lastName = Model.SearchTerm }))
.Model(model => model.Id(backup => backup.BackupUId))
)
)
<script>
function SelectBackupButtonClickHandler() {
var selectedBackup = this.dataItem(this.select());
console.log(selectedBackup);
}
$("#MatchingBackupsGrid").data("kendoGrid").bind("change", SelectBackupButtonClickHandler);
</script>
当你使用客户端的代码,你只能访问客户端的数据; 你不能访问使用强类型的模型grid.dataItem()
如果你想做到这一点,你需要商品的ID发送给服务器,例如,使用一个AJAX请求,并继续有你的工作; 所以你必须做这样的事情:
function SelectBackupButtonClickHandler() {
var grid = $("#MatchingBackupsGrid").data("kendoGrid");
var selectedBackup = grid.dataItem(grid.select());
var id = selectedBackup.id;
$.ajax({
type: "POST",
url: yourUrl, // post to your controller action that does what you want to do with the model
dataType: "json",
data: { id: id }
};
}
随着kendoHelpers你可以得到所选项目的DataItem的
// Get the grid
var grid = $('#SampleGrid').data('kendoGrid');
// Call your desired function
var dataItem = kendoHelpers.grid.getSelectedDataItem(grid);
// Manipulate the results
if (dataItem != null){
// dataItem.MyId = ...
}
另一种方法是getSelectedDataItemByCurrentCell
不需要网格为可选择的,并适用于活动单元格。
该库还具有许多其他的辅助功能。
我不知道那是什么,我是较早做,我的代码现在工作。
我试了很多东西-我认为由于某种原因grid.select()
返回一个数组,所以我试图申请一个索引得到像这样的数组的第一个元素:
var selectedBackups = grid.select();
// strangely, the Visual Studio intellisense mislead
// me here as it showed me the model properties on
// indexing this array
selectedBackups[0].BackupFirstName;
我也试过像这样的阵列上的转变:
var names = $.map(grid.select(), function(item)
{
return item.BackupFirstName + ' ' + item.BackupLastName;
});
我在这么多的想法,我想,我忘了检查我的原代码, 并有实际所选行的强类型模型之间。
所以,仅仅是为了记录,在问题的代码是正确的,它被转载如下。 要获得单一选择模式KendoUI电网所选择的项目,请执行以下操作:
var grid = $("#MatchingBackupsGrid").data("kendoGrid");
// this is a single object and not an array
// this is your strongly typed model object
var selectedBackup = grid.dataItem(grid.select());
console.log(selectedBackup);