【问题标题】:Binding Asp.net MVC model data to a kendo TreeView template (local data)将 Asp.net MVC 模型数据绑定到 kendo TreeView 模板(本地数据)
【发布时间】:2014-10-15 17:00:06
【问题描述】:

我正在使用模板在剑道树形视图中显示我的数据。目前,数据来自 Asp.net MVC 模型。我是剑道新手。我看到了各种用于绑定到本地数据的剑道示例,但我很困惑如何将本地数据绑定到剑道树视图中的模板内。

我知道这有点含糊。感谢您的及时回复。

任何简单的例子都会有很大帮助。

【问题讨论】:

    标签: c# templates kendo-ui treeview


    【解决方案1】:

    这是 ASP.NET MVC 和 Kendo UI 的基本示例。欲了解更多信息,请参阅 Telerik documentation

    查看

    <script id="TreeViewTemplate" type="text/kendo-ui-template">
        <div>
            <span style="background-color: Pink">#: item.text #</span>
            <span style="background-color: yellow">#: item.id #</span>
            <span style="background-color: Green">#: item.expanded #</span>
        </div>
    </script>
    
    
    @(
    
         Html.Kendo().TreeView()
                     .Name("TreeViewTemplateBiding")
                     .TemplateId("TreeViewTemplate")
                     .BindTo((IEnumerable<NodeViewModel>)ViewBag.Tree, (NavigationBindingFactory<TreeViewItem> mappings) =>
                                {
                                    mappings.For<NodeViewModel>(binding => binding.ItemDataBound((item, node) =>
                                    {
                                        item.Id = node.Id.ToString();
                                        item.Text = node.Title;
                                        item.Expanded = node.Expanded;
                                    })
                            .Children(node => node.Children));
                                })
    )
    

    控制器

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            var items = new List<NodeViewModel>();
    
            var root = new NodeViewModel { Id = 1, Title = "Root" };
            items.Add(root);
    
            root.Children.Add(new NodeViewModel { Id = 2, Title = "One" });
            root.Children.Add(new NodeViewModel { Id = 3, Title = "Two" });
    
            this.ViewBag.Tree = items;
    
            return View();
        }
    }
    
    public class NodeViewModel
    {
        public NodeViewModel()
        {
            this.Expanded = true;
            this.Children = new List<NodeViewModel>();
        }
    
        public int Id { get; set; }
        public string Title { get; set; }
        public bool Expanded { get; set; }
    
        public bool HasChildren
        {
            get { return Children.Any(); }
        }
    
        public IList<NodeViewModel> Children { get; private set; }
    }
    

    【讨论】:

    • 感谢 Feross.........抱歉耽搁了。你的例子确实帮助我获得了基本的理解。我之前的项目做得很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多