Treeview child nodes not working

2 posts, 0 answers
  1. Kabelo
    Kabelo avatar
    2 posts
    Member since:
    Jan 2015

    Posted 21 Oct 2015 Link to this post

    I have two classes Category and Product and I want to expend products when users click a category node on the treeview.

    My View is as follows:

        <h2>Index</h2>

    <p>
        @Html.ActionLink("Create New", "Create")
    </p>
    <div class="row">
        <div class="col-md-3">
            <label class="k-label-top">TreeView</label>
            @(Html.Kendo().TreeView().Name("treeview")
            .DataSource(datasource => datasource
                                .Read(read => read.Action("Categories", "Categories"))
                ).DataTextField("Name")
            )
        </div>
        
    </div>

     

    Controller

            public ActionResult Categories(int? id)
            {
                var model = db.Categories
                    .Where(p => p.Id == id.Value || p.Id == null)
                    .Select(p => new {
                        id = p.Id,
                        Name = p.Name,
                        hasChildren = p.Products.Any()
                    });
                return this.Json(model, JsonRequestBehavior.AllowGet);
            }

    Categories Class

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace EmployeeTreeView.Models
    {
        public class Category
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public virtual ICollection<Product> Products { get; set; }
        }
    }

     

    Products Class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    namespace EmployeeTreeView.Models
    {
        public class Product
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public virtual Category Category { get; set; }
            public int CategoryId { get; set; }
        }
    }

     When I remove the where clause in the controller the parent node just loops over and over again, but no child nodes are displayed.

  2. Kabelo
    Kabelo avatar
    2 posts
    Member since:
    Jan 2015

    Posted 22 Oct 2015 in reply to Kabelo Link to this post

    This question has been answer if you have a similar question checkout the answer first on stack overflow on this link. http://stackoverflow.com/questions/33259165/adding-parent-and-child-to-kendo-ui-controller-using-mvc-5/33260819#33260819
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top