在将 kendo treeview 与服务器端数据一起使用时,在 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 factory) at Kendo.Mvc.UI.NavigationItemContainerExtensions.Bind[TNavigationItem](TNavigationItem component, Object dataItem, NavigationBindingFactory 1 factory)
at Kendo.Mvc.UI.NavigationItemContainerExtensions.BindTo[TNavigationItem](INavigationItemContainer
1 component, IEnumerable dataSource, Action1 factoryAction)
at Kendo.Mvc.UI.Fluent.TreeViewBuilder.BindTo(IEnumerable dataSource, Action
1 factoryAction)在 ASP._Page_Areas_Setups_Views_Mapping_DayPart_cshtml.Execute() 在 d:\APISBI_MVC\APISBI\Main\SRC\Apis.Web.MvcPortal\Areas\Setups\Views\Mapping\DayPart.cshtml:System.Web.WebPages 的第 47 行。 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) at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) 在系统。Web.Mvc.ControllerActionInvoker.<>c_DisplayClass1a.b _17() 在 System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter 过滤器,ResultExecutingContext preContext,Func`1 延续)
提前致谢。