簡體   English   中英

Kendo Treeview綁定父母和孩子

[英]Kendo Treeview Binding parent and children

我正在嘗試在此處遵循此示例http://docs.telerik.com/kendo-ui/getting-started/using-kendo-with/aspnet-mvc/helpers/treeview/ajax-binding

但是,每當我嘗試修改其代碼時,都會收到一條錯誤消息:

錯誤2'Kendo.Mvc.UI.Fluent.ReadOnlyDataSourceBuilder'不包含'模型'的定義,並且找不到擴展方法'Model'接受類型為'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。卷筒紙

這是我的代碼

@(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);
        }

    }
}

您的視圖應定義一個模型,以使其起作用

將此行添加到視圖的頂部:

@model categories //或您的Entity @model categories任何命名空間

這應該工作正常!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM