【问题标题】:Kendo Treeview Binding parent and children剑道树视图绑定父母和孩子
【发布时间】:2014-08-05 18:29:49
【问题描述】:

我正在尝试在此处遵循此示例 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”类型的第一个参数找到(您是否缺少 using 指令或程序集参考?) 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);
        }

    }
}

【问题讨论】:

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


    【解决方案1】:

    你的视图应该定义一个模型以便它工作

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

    @model categories //或您的实体类别所在的任何命名空间

    这应该可以正常工作!

    【讨论】:

    • 所以如果这棵树在我的主布局文件中,我不使用模型,我应该如何绑定它?我应该创建一个部分表单还是什么?或者我可以在我的布局文件中以另一种方式绑定它吗?
    • 是的,最好将树视图放在单独的局部视图中,然后通过ajax调用让它与其他视图交互,这样您就可以将树视图逻辑与其他视图分开并且易于管理
    • 嘿,我用更多代码更新了我的原始帖子。你认为你可以看看,看看我做错了什么?谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-27
    • 2015-03-02
    • 1970-01-01
    相关资源
    最近更新 更多