Kendo TreeView序列不包含任何元素错误

时间:2013-07-18 07:17:25

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

将kendo树视图与服务器端数据一起使用时,在mvc

中绑定数据时会出现问题

我的控制器操作是:

public ActionResult DayPart(long storeid)
        {




            IEnumerable<TreeViewMapModel> objDaypart = from item in new DayPartService().GetAll().ToList()
                                                       select new TreeViewMapModel
                                                       {
                                                           Id = item.Id,
                                                           Name = item.Name,
                                                           Items = (from map in new DayPartMappingService().GetByStoreId(storeid)
                                                                    where item.Id == map.DayPartId
                                                                    select new MasterModel() { Id = int.Parse(map.SourceId), Name = map.SourceLabel }).ToList(),

                                                       };

 var ret = objDaypart.Select(x => new TreeViewMapModel
           {
               HasChildren = x.Items.Count() > 0,
               Id = x.Id,
               Items = x.Items ?? Enumerable.Empty<MasterModel>(),
               Name = x.Name
           });
@ViewBag.storename = new StoreService().GetById(storeid).Name;
          return View(ret);
        }

并在视图中

@model IEnumerable<Apis.Web.MvcPortal.Areas.Setups.Models.TreeViewMapModel>
 @(Html.Kendo().TreeView()
.Name("right-treeview")
.BindTo(Model.ToList(), (Kendo.Mvc.UI.Fluent.NavigationBindingFactory<TreeViewItem> mappings) =>
{
    mappings.For<Apis.Web.MvcPortal.Areas.Setups.Models.TreeViewMapModel>(bound => bound.ItemDataBound((node, structure) =>
    {
        node.HasChildren = structure.HasChildren;
        node.Id = structure.Id.ToString();
        node.Text = structure.Name;
        })

    .Children(structure => structure.Items));
})
        )

但我得到的错误序列不包含任何元素。任何人都可以告诉我做错了什么。

我的堆栈跟踪是:    在System.Linq.Enumerable.First [TSource](IEnumerable 1 source) at Kendo.Mvc.UI.NavigationItemContainerExtensions.Bind[TNavigationItem](TNavigationItem component, Object dataItem, NavigationBindingFactory 1工厂)    at Kendo.Mvc.UI.NavigationItemContainerExtensions.Bind [TNavigationItem](TNavigationItem组件,Object dataItem,NavigationBindingFactory 1 factory) at Kendo.Mvc.UI.NavigationItemContainerExtensions.BindTo[TNavigationItem](INavigationItemContainer 1组件,IEnumerable dataSource,Action 1 factoryAction) at Kendo.Mvc.UI.Fluent.TreeViewBuilder.BindTo(IEnumerable dataSource, Action 1 factoryAction)    在d:\ APISBI_MVC \ APISBI \ Main \ SRC \ Apis.Web.MvcPortal \ Areas \ Setups \ Views \ Mapping \ DayPart.cshtml中的ASP._Page_Areas_Setups_Views_Mapping_DayPart_cshtml.Execute()中:第47行    在System.Web.WebPages.WebPageBase.ExecutePageHierarchy()    在System.Web.Mvc.WebViewPage.ExecutePageHierarchy()    在System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage)    在System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object instance)    在System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext,TextWriter writer)    在System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)    在System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext,ActionResult actionResult)    在System.Web.Mvc.ControllerActionInvoker。&lt;&gt; c_ DisplayClass1a.b _17()    在System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter过滤器,ResultExecutingContext preContext,Func`1 continuation)

提前致谢。

1 个答案:

答案 0 :(得分:0)

当Children的对象数据类型也没有转换为传递给网格的相同类型时,我收到了类似的错误。

例如,在我的情况下,来自该服务的是一个List,我将其转换为更通用的List - 但坐在每个Tag上的孩子仍然是标签。

我需要一个递归函数将子标签转换为OurTreeItemClass,或者创建一个服务方法,首先将此数据作为OurTreeItemClass返回,以减轻完成这些转换的需要。