简体   繁体   中英

ASP.NET MVC Building a Child/Parent Tree

I've got this task requiring building a parent/child view in html. NOT USING ANY PLUGINS!

I've created a database and a model (H_Table), and a class (Element) in controller for child nodes. But how do I get this to work together. So, that it retrieves data from the model passes it to the class and returns to view as model. I'm not really sure whether I explaied it right. Ask away.

My Model:

namespace Tree_List.Models

{
    using System;
    using System.Collections.Generic;

    public partial class H_Table
    {
        public int ID { get; set; }
        public string NAME { get; set; }
        public Nullable<int> PARENT_ID { get; set; }
    }

}

My Controller:

using System.Collections.Generic;
using System.Web.Mvc;
using Tree_List.Models;

namespace Tree_List.Controllers
{
    public class ListController : Controller
    {
        // GET: List
        private Models.ListDBEntities1 db_connection = new Models.ListDBEntities1();

        public ActionResult Index()
        {
            var Items = db_connection.H_Table;

            return View(Items);
        }
    }

    public partial class Element
    {
        public int ID { get; set; }
        public string NAME { get; set; }
        public List<Element> CHILDS { get; set; }

    }
}

If I get your question right you want to render this hierarchical data of yours in a view. To do that you have to use Javascript and jQuery to render this data there are tons of ready libraries and open source examples on how to do this like:

https://www.codeproject.com/tips/696985/how-to-create-treeview-in-mvc

https://www.mindstick.com/Articles/1119/using-treeview-in-asp-dot-net-mvc

You can see them and write one yourself like them. Also take a look at more abstracted tool from Telerik .

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM