在 MVC 中将 DropDownList 添加到 kendo 网格

Adding DropDownList to kendo grid in MVC

我正在尝试借助此文档将下拉列表添加到 kendo 网格 : http://demos.telerik.com/aspnet-mvc/grid/editing-custom

实际上我遵循了完全相同的方式,但我不知道 kendo 网格如何理解它必须在 clientTemplate 中放置一个下拉列表?! clientTemplate 必须在某处定义吗?

您必须通过将其添加到网格来定义一个 clientTemplate .ClientDetailTemplateId("template")
然后你可以将 DropDownList 添加到模板中

<script id="template" type="text/kendo-tmpl">
    @(Html.Kendo().DropDownList()
       //build the dropdownlist
       .ToClientTemplate()
    )
</script>

演示:http://demos.telerik.com/aspnet-mvc/grid/detailtemplate

只是为了增加答案的组合,这是一个迟到的...

  • 在您的 ViewModel 中创建一个列表
  • 将您的 Model.PropertyId 视为外键

例如...

columns.ForeignKey(x => x.ChangeTypeId, Model.ChangeTypes, "Id", "ChangeTypeName")

示例视图:

@(Html.Kendo().Grid<ChangeRequest>()
              .Columns(columns =>
              {
                  columns.Bound(x => x.Id)
                      .Visible(false);
                  columns.Bound(x => x.Description)
                      .Title("Description")
                      .Width(100);
                  columns.ForeignKey(x => x.ChangeTypeId, Model.ChangeTypes, "Id", "ChangeTypeName")
                      .Title("Data Type")
                      .Width(50);
                  columns.Command(command => { command.Edit(); command.Destroy(); }).Width(100);
              })
              .Name("gridChangeRequest")
              .ToolBar(toolbar => toolbar.Create())
              .Editable(editable => editable.Mode(GridEditMode.InLine))
              .Pageable()
              .Sortable()
              .Scrollable()
              .BindTo(Model.RTUDeviceCustomRegisterModbuses)
              .DataSource(dataSource => dataSource.Ajax()
                                                  .ServerOperation(true)
                                                  .PageSize(50)
                                                  .Model(model => { model.Id(m => m.Id); })
                                                  .Create(update => update.Action("Create", "ChangeRequest", new { Area = "Documents" }))
                                                  .Update(update => update.Action("Update", "ChangeRequest", new { Area = "Documents" }))
                                                  .Destroy(update => update.Action("Destroy", "ChangeRequest", new { Area = "Documents" }))
                                                  )
              .HtmlAttributes(new { @class = "", @style = "height: 400px;" }))

示例视图模型:

public class ChangeRequestFormViewModel : ViewModelBase
{
    #region <Constructors>

    public ChangeRequestFormViewModel(IApplication application) : base(application)
    {
        InitializeCreateEmpty();
    }

    #endregion

    #region <Properties>

    public ChangeRequestDocument Entity { get; set; }

    #region lookups

    public List<ChangeType> ChangeTypes { get; set; }

    #endregion

    #endregion

    #region <Methods>

    private void InitializeCreateEmpty()
    {
        var builder = Application.ChangeRequestDocumentXmlDataSetBuilder; //<-- This object is specific to my (particular) application
        var dataset = builder.CreateEmpty();

        Entity = dataset.Form;

        ChangeTypes = dataset.ChangeTypes;
    }

    #endregion
}