Kendo Ui Grid中的自定义下拉列表

时间:2013-09-27 13:31:43

标签: kendo-ui kendo-grid kendo-asp.net-mvc

您好我正在尝试向Kendoui MVC网格添加自定义下拉列表。网格的所有示例都显示了如何使用外键关系执行此操作。我们是一个下拉列表,对数据执行操作(查看详细信息,编辑信息,删除记录),因此它与数据无关。所以在index.aspx中我有:

  <% Html.Kendo().Grid<Training.Models.TrainingViewManagementModel>()
.Name("grid")
.Columns(columns =>
    {
        columns.Bound(x => x.SelectAction).Width(95).Title("Select Action").ClientTemplate("#=SelectAction#");
        columns.Bound(x => x.Record).Width(100);
        columns.Bound(x => x.Code).Width(65);
        columns.Bound(x => x.PeopleTrained).Width(75);
        columns.Bound(x => x.TrainingTypes).Width(100);
        columns.Bound(x => x.Trainer).Width(100);
        columns.Bound(x => x.TrainingDate).Format("{0:MM/dd/yyyy}").Width(100);
    })

.Editable(editable => editable.Mode(GridEditMode.InLine))
.Pageable()
.Sortable()
.Scrollable()
.HtmlAttributes(new { style = "height:500px;" })
.DataSource(dataSource => dataSource
        .Ajax()            
        .PageSize(50)
        .Read("RetrieveTrainingManagementGrid", "Home")
                .Model(m =>
                {
                    m.Id(x => x.TrainingID);
                    m.Field(x => x.SelectAction).Editable(true);
                    m.Field(x => x.Record).Editable(false);
                    m.Field(x => x.Code).Editable(false);
                    m.Field(x => x.PeopleTrained).Editable(false);
                    m.Field(x => x.TrainingTypes).Editable(false);
                    m.Field(x => x.Trainer).Editable(false);
                    m.Field(x => x.TrainingDate).Editable(false);
                })
        ).Render();
%>

然后由于示例代码,我有以下编辑器模板:

<%=Html.Kendo().DropDownListFor(m=>m)
    .Name("SelectAction")
    .Events(e=>e.Change("onGridchange"))
    .DataTextField("DropDownName")
    .DataValueField("DropDownID")
    .DataSource(datasource =>datasource
        .Read("RetrieveDropdownOptionsKendo", "Home"))

 %> 

然后在模型中我确信我传递了正确的数据

public IEnumerable<TrainingViewManagementModel> RetrieveAirportManagementView()
        {
            return new List<TrainingViewManagementModel>()
            {
               new TrainingViewManagementModel {
                SelectAction = new List<DropDownOptions> { new DropDownOptions { DropDownID = 0, DropDownName = "Select an action"}},
                TrainingID = 561,
                Record = "2001-xxx",
                ID = 206,
                Code = "BMW",
                PeopleTrained = 0,
                TrainingTypes = "SCRUM, Hi",
                UserID = new Guid(),
                Trainer = "John dowle",
                TrainingDate = DateTime.Parse("2009-11-21"),
                IndividualPeople = "Bob Jim, Jim bob, Jane Bob"
            }
            };
        }

当我运行代码时,我在下拉列中得到了这个[object Object]。我知道我错过了一些东西,但我从样本和文档中得到了相互矛盾的信息。提前致谢。

2 个答案:

答案 0 :(得分:0)

RetrieveAirportManagementView方法必须返回JsonResult的类型:

public JsonResult RetrieveAirportManagementView()
    {
        var list= new List<TrainingViewManagementModel>()
        {
           new TrainingViewManagementModel {
            SelectAction = new List<DropDownOptions> { new DropDownOptions { DropDownID = 0, DropDownName = "Select an action"}},
            TrainingID = 561,
            Record = "2001-xxx",
            ID = 206,
            Code = "BMW",
            PeopleTrained = 0,
            TrainingTypes = "SCRUM, Hi",
            UserID = new Guid(),
            Trainer = "John dowle",
            TrainingDate = DateTime.Parse("2009-11-21"),
            IndividualPeople = "Bob Jim, Jim bob, Jane Bob"
        }
        };return Json(list, JsonRequestBehavior.AllowGet);
    }

答案 1 :(得分:0)

我遇到了完全相同的问题,我正在努力寻找解决方案。最后,我设法使用稍微不同的方法使其工作。我没有使用DropDownList,而是在列中使用垂直菜单作为客户端模板。更多细节。我有一个这样的模型产品

public class Product
    {

        [AutoIncrement]
        [Alias("id")]
        [DataMember]
        public int Id { get; set; }

        [DataMember]
        public string Name { get; set; }


        [DataMember]
        [ForeignKey(typeof(Store), OnDelete = "CASCADE")]
        public int StoreId { get; set; }

        [DataMember]
        [Reference]
        public Store Store { get; set; }


    }

在网格上我想显示产品的ID,名称,商店模型的外键StoreId,商店的名称以及包含&#34;&Product 34等选项的列#34;,&#34; Open Store&#34;等。使用这种方法http://docs.telerik.com/kendo-ui/aspnet-mvc/helpers/grid/faq#how-do-i-use-a-kendo-ui-widget-inside-a-grid-client-column-template?我最终得到了这个

@(Html.Kendo().Grid<PixieKendoMVC.Models.Product>()
    .Name("grid")
    .DataSource(dataSource => dataSource
            .Ajax()
            .Batch(true)
            .ServerOperation(false)
            .PageSize(20)
                .Events(events => events.Error("onGridError"))
           .Model(model =>
            {
                model.Id(p => p.Id);

            })
            .Read(read => read.Action("Get", "Product" ))
            .Create(create => create.Action("Create", "Product"))
            .Update(update => update.Action("Edit", "Product"))
            .Destroy(destroy => destroy.Action("delete", "Product"))
    )
                 .ToolBar(toolBar =>
                    {
                        toolBar.Create();
                        toolBar.Save();
                    })

    .Columns(columns =>
    {
        columns.Template(p => { }).Width(80).HtmlAttributes(new { @class = "templateCell" }).ClientTemplate(
                Html.Kendo().Menu().Orientation(MenuOrientation.Vertical)
                    .Name("menu_#=Id#")
                    .Items(its =>
                    {
                        its.Add().Text("do").Items(nested =>
                        {
                            nested.Add().Text("Action 1");
                            nested.Add().Text("Action 2");
                            nested.Add().Text("Action 3");
                        });

                    })
                    .ToClientTemplate().ToHtmlString()
                );

        columns.Bound(p => p.Id);
        columns.Bound(p => p.Name);
        columns.Bound(p => p.StoreId);
        columns.ForeignKey(p => p.StoreId, (System.Collections.IEnumerable)ViewData["stores"], "Id", "Name")
           .Title("Store").Width(150);

        columns.Command(command => command.Destroy()).Width(110);

    })
        .Events(ev => ev.DataBound("initMenus"))

    .Editable(editable => editable.Mode(GridEditMode.InCell))
    .Pageable()
    .Sortable()
    .Scrollable()
    .Filterable()
)
<span id="notification"></span>

<style type="text/css">
    .k-widget .templateCell {
        overflow: visible;
        width:80px;
        margin:10px;
        padding:10px;
    }

</style>

<script>
    function initMenus(e) {
        $(".templateCell").each(function () {
            eval($(this).children("script").last().html());
        });
    }
</script>

现在,您可以在各种菜单项上添加操作,而不仅仅是文本。还需要一些CSS样式,因此欢迎更正