0

我正在使用 Kendo MVC Grid,我想在其中一个单元格中有一个下拉列表。这是我的代码:

    @(Html.Kendo().Grid<RMS.Admin.ViewModel>()
  .Name("ResourceGrid")
  .Columns(columns =>
  {
      columns.Bound(c => c.ResourceName);
      columns.Bound(c => c.Descritption);
      columns.Bound(c => c.ResourceType).ClientTemplate("#=ResourceType#");
      columns.Bound(c => c.Approved);
      columns.Bound(c => c.IsEnabled);
      columns.Bound(c => c.Data).Width(220);
      columns.Command(command =>
      {
          command.Edit();
          command.Destroy();
      }).Width(172).Title("Edit/Delete");
  })
  .ToolBar(toolbar => toolbar.Create())
  .Editable(editable => editable.Mode(GridEditMode.InLine))
  .Scrollable()
  .Sortable()
      .HtmlAttributes(new { style = "height:800px" })
  .Pageable(pageable => pageable
      .Refresh(true)
      .PageSizes(true)
      .ButtonCount(5))
      .ClientDetailTemplateId("template")
  .DataSource(dataSource => dataSource
      .Ajax()
      .Model(model =>
      {
          model.Id(s => s.ResourceId);
          model.Field(p => p.ResourceType).DefaultValue(ViewData["defResourceType"] as RMS.Admin.ViewModel.ResourceTypeId);
      })
                .Create(update => update.Action("CreateResource", "Home", new { resourceTypeId = "#=ResourceType.Id#" }))
                .Read(read => read.Action("ReadResource", "Home"))
                .Update(update => update.Action("UpdateResource", "Home"))
                .Destroy(destroy => destroy.Action("RemoveResource", "Home"))

    )
        .Events(events => events.DataBound("dataBound"))
  )

问题是我不知道客户端模板是什么,所以我不知道如何处理它。如果将资源类型与客户端模板绑定,我无法将新记录添加到网格中,我会收到错误消息:未捕获的引用错误:资源类型未定义

如果我删除客户端模板,我可以添加一条记录,但是当我尝试保存它时,它说它找不到 ResourceType 的 id。

4

1 回答 1

1

基本上,如果您想在 kendo ui 网格中使用下拉列表,则必须定义一个包含 kendo 下拉列表小部件的编辑器模板。这意味着您必须使用属性 .EditorTemplateName() 声明相应的单元格,如下所示:

.Columns(columns =>
  {
      columns.Bound(c => c.ResourceName);
      columns.Bound(c => c.Descritption);
      columns.Bound(c => c.ResourceType).EditorTemplateName("TemplateName");
      columns.Bound(c => c.Approved);
      columns.Bound(c => c.IsEnabled);
      columns.Bound(c => c.Data).Width(220);
      columns.Command(command =>
      {
          command.Edit();
          command.Destroy();
      }).Width(172).Title("Edit/Delete");
  })

编辑器模板必须存储在文件夹中:Shared/EditorTemplates/TemplateName.cshtml

TemplateName.cshtml 应如下所示:

@model ModelName

@(Html.Kendo().DropDownList()
.OptionLabel("please select a value")    
.Name("ResourceType") <-- the name needs to be equal to the grid cell name
    .DataSource(source=>
        {
        source.Read( read =>
            {
                read.Action("ActionName", "ControllerName");

            })
            .ServerFiltering(true);
        })
    .DataTextField("Name") <-- displays the text of the object
    .DataValueField("ID") <-- stores the id of the object
    )

希望这将引导您走向正确的方向。干杯,

于 2014-09-24T07:03:21.867 回答