简体   繁体   English

在ASP.NET Core中使用无序列表创建树

[英]Create tree with unordered list in ASP.NET Core

I have: 我有:

  • Class Person 类人
  • A person with a list of children ( List<Person> children ) 带有孩子List<Person> childrenList<Person> children
  • The tree is imported into the Razor page 该树将导入到“剃刀”页面中

I am trying to create a tree in a Razor page with this. 我正在尝试以此在Razor页面中创建树。 I have tried doing it with only Razor code, like seen below: 我尝试仅使用Razor代码来执行此操作,如下所示:

<div class="container" style="padding-top:5%;">
        <div class="tree">

            @helper ShowTree(Person root)
            {
                <ul>
                    <li>
                        <a href="#">@root.Name</a>
                        @if (root.children.Count() > 0)
                        {
                            foreach (var child in root.children)
                            {
                                @Show(child)
                            }
                        }
                    </li>
                </ul>
            }

        </div>
    </div>

    @helper Show(Person child)
    {
        <ul>
            <li><a href="#">@child.Name</a></li>
            @if (child.children.Count() > 0)
            {
                foreach (var c in child.children)
                {
                    Show(c);
                }
            }
        </ul>
    }

    {
        @ShowTree(Model);
    }

As can be seen I try to nest children inside the parent with unordered list recursively. 可以看出,我尝试将子级递归地嵌套在具有无序列表的父级中。 And I have to do it like this in order for the CSS to work, example below: 为了使CSS正常工作,我必须这样做,例如以下示例:

<ul><li><a href="#">Root</a> 
        <ul><li><a href="#">Root.Child</a></li></ul> 
</li></ul>

For some reason it didn't want to print grand children. 由于某种原因,它不想打印孙子。 I don't get why. 我不明白为什么。

Question 1: anybody got an idea why it skips to print grandchildren? 问题1:有人知道为什么不打印孙子孙女吗?


Secondly, I tried to do the same in JavaScript. 其次,我尝试在JavaScript中执行相同的操作。 However, I don't know how to insert the Razor Model into JavaScript. 但是,我不知道如何将Razor模型插入JavaScript。 I just get an error message saying "is not defined at INDEX". 我只是收到一条错误消息,提示“未在INDEX定义”。 I will also insert the code below if that will help: 如果有帮助,我还将在下面插入代码:

<script type="text/javascript">
    var root= Model;
    var html = "";

    function Show(child) {
        html += "<ul>";
        html += "<li><a href='#'>";
        html += child.Name;
        html += "</a >";
        if (child.children.Count() > 0) {
            for (var c in child.children) {
                Show(c)
            }
        }
        html += "</li></ul>";
    }

    function ShowTree(root) {
        html += "<ul>";
        html += "<li>";
        html += "<a href='#'>";
        html += root.Name;
        html += "</a>";
        if (root.children.Count() > 0) {
            for (var child in root.children) {
                Show(child)
            }
        }
        html += "</li>";
        html += "</ul>";

    }

    ShowTree(root);

    $("div.container > div.tree ul").append(html);

</script>

Question 2: is there any way to insert Model into the script tag? 问题2:有什么方法可以将Model插入script标签? I read about <text> tags but they didn't help me at all. 我读到有关<text>标记的信息,但它们根本没有帮助我。

is there any way to insert Model into the script tag? 有什么方法可以将Model插入script标签?

you can easily inject your viewmodel into a view file: 您可以轻松地将视图模型注入视图文件中:

@model SO.Models.Person

and now you can get the json representation of Model via : 现在您可以通过以下方式获取Model的json表示形式:

 <script> var root = @Html.Raw(Json.Serialize(Model)) ; // … show root </script> 

when downloaded from server , the javascript looks like : 从服务器下载时,javascript如下所示:

 <script> var root = {"id":1,"name":"root.Name","children":[{"id":2,"name":"c1.Name","children":[]},{"id":3,"name":"c2.Name","children":[]},{"id":4,"name":"c3.Name","children":[]}]} ; </script> 

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

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