在MVC中向kendo网格添加下拉列表框

3
我正在尝试通过这份文档的帮助,在Kendo网格中添加一个下拉列表。
实际上,我完全按照文档所述的方法进行了操作,但是无法成功。我想知道Kendo网格是如何理解在clientTemplate中放置下拉列表的?是否需要在其他地方定义clientTemplate?
2个回答

1

您需要通过在Grid中添加以下内容来定义一个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


我想把下拉列表放在我的表格的一列中,比如说第三列,那么在这种情况下,建议的解决方案如何知道在哪里放置下拉列表? - user4092086

0

为了增加答案的多样性,这里来一个晚一点的...

  • 在你的ViewModel中创建一个List
  • 将你的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
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接