簡體   English   中英

將Asp.net MVC模型數據綁定到Kendo TreeView模板(本地數據)

[英]Binding Asp.net MVC model data to a kendo TreeView template (local data)

我正在使用模板在劍道樹視圖中顯示數據。 當前,數據來自Asp.net MVC模型。 我是劍道新手。 我看到了各種用於綁定到本地數據的kendo示例,但是對於如何在kendo樹視圖中的模板內綁定本地數據感到困惑。

我知道這有點含糊。 感謝您的及時回應。

任何簡單的例子都可以提供很大幫助。

這是ASP.NET MVC和Kendo UI的基本示例。 有關更多信息,請參閱Telerik 文檔。

視圖

<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; }
}

暫無
暫無

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

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