无法获得动态下拉列表以在Kendo网格行中工作

时间:2019-04-05 18:13:26

标签: drop-down-menu kendo-grid kendo-asp.net-mvc

我试图将Kendo网格中的下拉列表限制为仅包含先前映射到该行另一单元格中所选公司的产品。

我使用了动态下拉式编辑器模板方法。

但是,传递给getCompanyId()的ID始终为null,因此我的下拉菜单始终为null。

查看:

@(Html.Kendo().Grid<XXXAppXXX.Models.WeeklyRailPlan>()
      .Name("grid")
      .Columns(columns =>
      {
          columns.Bound(c => c.WeekNumber);
          columns.Bound(c => c.Company).ClientTemplate("#=(data.Company) ? Company.Name : 'Select Company...'#");
          columns.Bound(c => c.ServiceCode);
          columns.Bound(o => o.Product)
            .ClientTemplate("#= (data.Product) ? Product.Name : 'Select Product'#")
            .EditorTemplateName("DynamicDropDownList");
   //etc
      })
      .ToolBar(toolbar => {
          toolbar.Create();
          toolbar.Save();
      })
      .Editable(editable => editable.Mode(GridEditMode.InCell))
      .Pageable()
      .Filterable()
      .Events(ev => ev
             .Remove(@"function(e){setTimeout(function(){$('#grid').data('kendoGrid').dataSource.sync()})}")
                            )
      .Sortable(sortable => {
          sortable.SortMode(GridSortMode.SingleColumn);
      })

      .Filterable()
      .DataSource(dataSource => dataSource
          .Ajax()
          .Events(events => events.Error("error_handler"))
          .Sort(p => { p.Add("WeekNumber").Descending(); })
          .Model(model => model.Id(p => p.ID))
          .Read(read => read.Action("WeeklyRailPlans_Read", "WeeklyRailPlanGrid"))
          .Create(create => create.Action("WeeklyRailPlans_Create", "WeeklyRailPlanGrid"))
          .Update(update => update.Action("WeeklyRailPlans_Update", "WeeklyRailPlanGrid"))
          .Destroy(destroy => destroy.Action("WeeklyRailPlans_Destroy", "WeeklyRailPlanGrid"))
      )
)

称为DynamicDropDownList.cshtml的EditorTemplate

<script type="text/javascript">
    function getCompanyId() {
        return { CompanyID: '#=ID#' };
    }
</script>


@(Html.Kendo().DropDownList()
                .Name("Product")
                .DataValueField("ID")
                .DataTextField("Name")
                .DataSource(ds => ds
                    .Read(read => read.Action("GetProductsForCompany", "Products").Data("getCompanyId")))
)    

控制器方法GetProductsForCompany(此方法始终接收null)

        public ActionResult GetProductsForCompany(int CompanyID)
        {
            return Json(db.Products.Where(e => e.Companies.Any(t =>t.ID == CompanyID)), JsonRequestBehavior.AllowGet);
        }

2 个答案:

答案 0 :(得分:1)

我使用这样的代码:

<script type="text/javascript">
    function getCompanyId() {
        var gview = $('#grid').data("kendoGrid");
        var selectedItem = gview.dataItem(gview.select());
        return { CompanyID: selectedItem.ID };
    }
</script>

答案 1 :(得分:0)

此解决方案要求为:

function getCompanyId() {
    var grid = $('#grid').data('kendoGrid');
    var dataItem = grid.dataItem(grid.table.find('.k-edit-cell').parents('tr'))
    return { CompanyID: dataItem.Company.ID };
}