Kendo Treeview绑定父母和孩子

时间:2014-08-05 18:29:50

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

我试图在这里遵循这个例子 http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/treeview/ajax-binding

但是,每当我尝试修改代码时,都会收到错误消息

错误2' Kendo.Mvc.UI.Fluent.ReadOnlyDataSourceBuilder'不包含'模型'的定义没有扩展方法'模型'接受类型' Kendo.Mvc.UI.Fluent.ReadOnlyDataSourceBuilder'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)c:\ Users \ Michael \ Google Drive \ Work \ Companies \ Clickable Community \ dhvs \ Clickable Community \ Development \ Portal \ ClickableCommunity.Web \ Views \ Shared_Layout。 cshtml 34 ClickableCommunity.Web

这是我的代码

@(Html.Kendo().TreeView()
                        .Name("treeview")
                        // The property that specifies the text of the node
                        .DataTextField("Name")
                        .DataSource(dataSource => dataSource
                            .Model(model => model
                                // The property that uniquely identieis a node.
                                // The value of this property is the argument of the action method
                                .Id("Id")
                                // the boolean property that tells whether a node has children
                                .HasChildren("HasChildren")
                            )
                            .Read(read => read
                                // The action method which will return JSON
                                .Action("ReadCats", "Home")
                            )
                        )
                    )

以及我在控制器中做的事情

public JsonResult ReadCats()
{
    var categories = _entityLogic.GetActiveCategories();
    var jsonResult = categories.Select(cat => new
            {
                Id = cat.Id,
                Name = cat.Name,
                HasChildren = categories.Where(c => c.ParentCategory == cat.Id).Any(),
                ParentId = cat.ParentCategory
            }).ToList();
    return Json(jsonResult, JsonRequestBehavior.AllowGet);
}

基本上,我有一个存储和ID,名称和父类别(父类别的ID)的表,我试图将树视图绑定到我所有的父子节点。提前致谢。

修改 所以我仍然无法得到这个。我一直得到同样的错误。我无法放置@model命名空间,因为它给我一个错误,说它是一个命名空间,但是像一个类型一样使用。这是我的整个代码。谢谢,

@model ClickableCommunity.Web.Models.Public.HomeModel

@using ClickableCommunity.Core.Models.Data
@using Kendo.Mvc.UI
@{
    ViewBag.Title = "Home Page";
}

<h2>@ViewBag.Message</h2>

    @(Html.Kendo().TreeView()
                            .Name("treeview")
                            // The property that specifies the text of the node
                            .DataTextField("Name")
                            .DataSource(dataSource => dataSource
                                .Model(model => model
                                    // The property that uniquely identieis a node.
                                    // The value of this property is the argument of the action method
                                    .Id("Id")
                                    // the boolean property that tells whether a node has children
                                    .HasChildren("HasChildren")
                                )
                                .Read(read => read
                                    // The action method which will return JSON
                                    .Action("ReadCats", "Home")
                                )
                            )
                        )

<ul>
@foreach (var item in Model.CategoryEntities)
{
    <li>
        @item.Name
    </li>

}
</ul>

这是我的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ClickableCommunity.Core.Contracts.Logging;
using ClickableCommunity.Core.Contracts.Logic;
using ClickableCommunity.Web.Models.Public;
namespace ClickableCommunity.Web.Controllers
{
    public class HomeController : BaseController
    {
        private readonly IEntityLogic _entityLogic;
        private readonly IGeogrpaphyLogic _geoLogic;
        private readonly IUserLogic _userLogic;

        public HomeController(ISystemLogger logger, IEntityLogic entityLogic, IUserLogic userLogic, IGeogrpaphyLogic geoLogic) : base(logger)
        {
            _entityLogic = entityLogic;
            _userLogic = userLogic;
            _geoLogic = geoLogic;
        }

        public ActionResult Index()
        {

            var catEnts = new List<HomeModel.CategoryEntitiesList>();

            var model = new HomeModel
            {
                AvailableCategories = _entityLogic.GetActiveCategories()
                , Entities = _entityLogic.GetActiveEntities()
                , States = _geoLogic.GetAllStates()
            };
            var tempCe = new HomeModel.CategoryEntitiesList();
            foreach (var i in model.AvailableCategories.Where(c => c.ParentCategory == null))
            {
                tempCe = new HomeModel.CategoryEntitiesList();
                tempCe.Name = i.Name;
                tempCe.ParentCategory = i.ParentCategory;
                tempCe.Id = i.Id;
                tempCe.HasChildren = model.AvailableCategories.Where(a => a.ParentCategory == i.Id).Any();
                catEnts.Add(tempCe);
            }
            model.CategoryEntities = catEnts;
            return View(model);

            //return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }


        public JsonResult GetChildTreeViewData(int? id)
        {
            var categories = _entityLogic.GetActiveCategories();
            if (id != null)
            {
                categories = categories.Where(c => c.ParentCategory == id);
            }
            var jsonResult = categories.Select(cat => new
                    {
                        Id = cat.Id,
                        Name = cat.Name,
                        HasChildren = categories.Where(c => c.ParentCategory == cat.Id).Any(),
                        ParentCategory = cat.ParentCategory
                    }).ToList();
            return Json(jsonResult, JsonRequestBehavior.AllowGet);
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您的视图应定义模型以使其正常工作

将此行添加到您的视图顶部:

@model categories //或您的实体类别中的任何名称空间

这应该可以正常工作!!