【发布时间】: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