【发布时间】:2015-05-28 04:50:47
【问题描述】:
我尝试按照示例将外键放入网格中。我遵循了这个例子:
http://demos.telerik.com/aspnet-mvc/grid/foreignkeycolumn
我稍微修改了一下,因为我想使用Popup Editor。
这是View 中的Grid 实现。
@(Html.Kendo().Grid<Example.Web.UI.ViewModels.ExampleItem>()
.Name("ExampleGrid")
.Columns(columns =>
{
columns.Bound(p => p.Name);
columns.Bound(p => p.Label);
columns.Bound(p => p.Type);
columns.Bound(p => p.InputType);
columns.ForeignKey(p => p.ParentId, (System.Collections.IEnumerable)ViewData["items"], "Id", "Name").Title("Parent");
columns.Command(command => { command.Edit(); command.Destroy(); });
})
.Scrollable()
.Groupable()
.Sortable()
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(true)
.ButtonCount(5))
.ToolBar(toolbar => toolbar.Create())
.Editable(editable => editable.Mode(GridEditMode.PopUp))
.DataSource(dataSource => dataSource
.Ajax()
.PageSize(20)
.Events(events => events.Error("error_handler"))
.Model(model =>
{
model.Id(p => p.Id);
model.Field(p => p.Id).DefaultValue(Guid.NewGuid());
model.Field(p => p.ParentId).DefaultValue(null);
})
.Create(update => update.Action("EditingPopup_Create", "Example"))
.Read(read => read.Action("EditingPopup_Read", "Example"))
.Update(update => update.Action("EditingPopup_Update", "Example"))
.Destroy(update => update.Action("EditingPopup_Destroy", "Example"))
)
)
这是ExampeItem 型号:
public class ExampleItem
{
[ScaffoldColumn(false)]
public Guid Id { get; set; }
public string Name { get; set; }
public string Label { get; set; }
public string Type { get; set; }
[DisplayName("Input Type")]
public string InputType { get; set; }
public ExampleItem Parent { get; set; }
public Guid ParentId { get; set; }
}
在我的Controller 中,我这样设置外键项:
ViewData["items"] = exampleItems; // This is a List<ExapleItem>
但由于某种原因,Grid 加载时Parent 列为空。
当我点击Edit 时,会弹出一个窗口并显示父级的 Guid。
Guid 应该是项目的下拉列表。而Parent 列应显示Parent 项目的名称。
此网格的想法是您可以将项目添加到网格中,并且当您这样做时,您可以选择所有已添加的网格项目中的任何一个作为父项。然后在Grid 中创建一个层次结构,稍后可以通过Parent 对其进行排序。
有人知道我哪里出错了吗?
【问题讨论】:
-
检查文件夹
~\Views\Shared\EditorTemplates` is there a file nameGridForeignKey.cshtml` ? -
是的,确实有,我从 KendoUI 附带的示例中复制了它
-
Guid应该有效。你的ViewBag["items"]是什么数据类型?您可能需要将其转换为IEnumerable<T>而不仅仅是IEnumerable。 -
它属于
ExapleItem,最初拼写错误。我确实以多种不同的方式进行了投射。我们做出了一个商业决定,即使用 KenodUI 并改用其他网络工具。
标签: asp.net-mvc kendo-ui telerik kendo-grid kendo-asp.net-mvc