【发布时间】:2014-12-15 14:27:14
【问题描述】:
我正在尝试使用 column.Bound 在 Telerik mvc 网格中添加下拉列表。我可以让下拉列表显示,但最初它显示为文本框。显然,如果我使用编辑器模板,它应该可以工作,但我得到错误值不能为空?
目的是在网格中显示一个下拉列表,每个项目将有不同的文本颜色和背景。这需要通过模型属性填充。
目前,我使用 ViewData 只是为了让事情正常进行,但并不快乐。被告知这可以通过模板来完成。
任何想法为什么这不起作用?
@using System.Collections.Generic;
@model IEnumerable<TelerikChecklist.Models.ProductViewModel>
@(Html.Kendo().Grid(Model)
.Name("gridDropDown")
.Columns(columns =>
{
columns.Bound(p => p.ProductName);
//columns.ForeignKey(p => p.CategoryID, (System.Collections.IEnumerable)ViewData["categories"], "CategoryID", "CategoryName")
// .Title("Category")
// .Width(150);
columns.Bound(p => p.CategoryID).Title("Category Name").ClientTemplate((@Html.Kendo().DropDownList()
.Name("dropdown_#=CategoryID#")
.BindTo((System.Collections.IEnumerable)ViewData["categories"])
.DataTextField("CategoryName")
.DataValueField("CategoryID")
.ValueTemplate("")
.ToClientTemplate().ToString()
)).EditorTemplateName("GridForeignKey");
})
.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable()
.Scrollable()
.HtmlAttributes(new { style = "height:250px;" })
.DataSource(dataSource => dataSource
.Ajax()
.Batch(true)
.PageSize(20)
.ServerOperation(false)
.Events(events => events.Error("errorHandler"))
.Model(model =>
{
model.Id(p => p.ProductID);
model.Field(p => p.ProductID).Editable(false);
model.Field(p => p.CategoryID).DefaultValue(1);
})
.Read(read => read.Action("ForeignKeyColumn_Read", "Home"))
.Update(update => update.Action("ForeignKeyColumn_Update", "Home")).Events(e => e.Change("Category"))
.Create(create => create.Action("ForeignKeyColumn_Create", "Home"))
.Destroy(destroy => destroy.Action("ForeignKeyColumn_Destroy", "Home"))
)
)
GridForeignKey.cshtml
@model object
@(
Html.Kendo().DropDownListFor(m => m)
.BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
)
【问题讨论】:
-
通过更新 GridForeignKey 以接受 Ienumerable 而不是选择列表来修复空值。
-
使用以下链接获取要显示的网格:stackoverflow.com/questions/22030725/…
标签: kendo-ui telerik kendo-grid