简体   繁体   English

ASP.NET MVC构建子/父树

[英]ASP.NET MVC Building a Child/Parent Tree

I've got this task requiring building a parent/child view in html. 我已经完成了需要在html中建立父/子视图的任务。 NOT USING ANY PLUGINS! 不使用任何插件!

I've created a database and a model (H_Table), and a class (Element) in controller for child nodes. 我在控制器中为子节点创建了一个数据库和一个模型(H_Table),以及一个类(Element)。 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: 为此,您必须使用Javascript和jQuery呈现此数据,其中有大量现成的库和有关如何执行此操作的开源示例,例如:

https://www.codeproject.com/tips/696985/how-to-create-treeview-in-mvc 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 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 . 还可以看一下Telerik的更抽象的工具。

Hope it helps :) 希望能帮助到你 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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