c# - 在 Razor View 中遍历列表

标签 c# asp.net asp.net-mvc razor

我有一个具有多个级别的 C# list,如下所示:

enter image description here

在我看来,我想做的是显示列表中的所有项目,而不仅仅是 parent 。我当前代码的结果是:

enter image description here

所以,那些只是 parent 。

我的观点:

@model IEnumerable<Docks.Base.Models.RootObject>

@{
    ViewBag.Title = "Docs Index";
    Layout = "~/Views/_Layout.cshtml";
}


<div class="page-wrapper-inside">
    <div class="groups">
        <div class="group-wrapper">
            <h1 class="group-title">Manage Navigation</h1>
            <div class="group">
                <div class="blocks-wrapper row">
                    <h1 class="intranet-group-title">Overview</h1>
                    <table class="table full-width">
                        <thead>
                            <tr>
                                <th>Id</th>
                                <th>Name</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model)
                            {
                                <tr>
                                    <td>@Html.DisplayFor(modelItem => item.id)</td>
                                    <td>@Html.DisplayFor(modelItem => item.name)</td>
                                    <td>
                                        @Html.ActionLink("Edit", "Edit", new { id = item.id }) |
                                        @Html.ActionLink("Details", "Details", new { id = item.id }) |
                                        @Html.ActionLink("Delete", "Delete", new { id = item.id })
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

我的模型:

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

namespace Docks.Base.Models
{

    public class Child
    {
        #region Properties
        public int id { get; set; }
        public int parentId { get; set; }
        public int typeId { get; set; }
        public int sortOrder { get; set; }
        public int authScope { get; set; }
        public string name { get; set; }
        public string area { get; set; }
        public object action { get; set; }
        public object controller { get; set; }
        public object root { get; set; }
        public object root_Included { get; set; }
        public object runLocation { get; set; }
        public string iconName { get; set; }
        public bool read { get; set; }
        public bool edit { get; set; }
        public bool add { get; set; }
        public bool delete { get; set; }
        public bool details { get; set; }
        public bool search { get; set; }
        public bool childDependent { get; set; }
        public bool parentDependent { get; set; }
        public bool blocked { get; set; }
        public int count { get; set; }
        public bool beginGroup { get; set; }
        public int itemType { get; set; }
        public string url { get; set; }
        public bool isInternal { get; set; }
        public List<Child> childs { get; set; }
        #endregion
    }

    public class RootObject
    {
        #region Properties
        public int id { get; set; }
        public object parentId { get; set; }
        public int typeId { get; set; }
        public int sortOrder { get; set; }
        public int authScope { get; set; }
        public string name { get; set; }
        public string area { get; set; }
        public object action { get; set; }
        public object controller { get; set; }
        public object root { get; set; }
        public object root_Included { get; set; }
        public object runLocation { get; set; }
        public string iconName { get; set; }
        public bool read { get; set; }
        public bool edit { get; set; }
        public bool add { get; set; }
        public bool delete { get; set; }
        public bool details { get; set; }
        public bool search { get; set; }
        public bool childDependent { get; set; }
        public bool parentDependent { get; set; }
        public bool blocked { get; set; }
        public int count { get; set; }
        public bool beginGroup { get; set; }
        public int itemType { get; set; }
        public object url { get; set; }
        public bool isInternal { get; set; }
        public List<Child> childs { get; set; }
        #endregion
    }

}

我的 Controller :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Docks.Base.Models;
using Newtonsoft.Json;

namespace Docks.Base.Controllers.Api
{
    public class NavigationController : Controller
    {
        // GET: Navigation
        public ActionResult Index()
        {
            string json = System.IO.File.ReadAllText(@"C:\develop\spl_development\main\Docks\Docks.Base\App\Data\LeftBar\data.json");
            List<RootObject> list = JsonConvert.DeserializeObject<List<RootObject>>(json);

            return View(list);
        }
    }
}

我希望我把我的问题说清楚,并且有人有解决方案,在此先感谢!

最佳答案

你可以通过编写内联函数并递归地调用它来做到这一点

        @helper PopulateChild(Child child)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => child.id)</td>
                <td>@Html.DisplayFor(modelItem => child.name)</td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id = child.id }) |
                    @Html.ActionLink("Details", "Details", new { id = child.id }) |
                    @Html.ActionLink("Delete", "Delete", new { id = child.id })
                </td>
            </tr>
            foreach (var item in child.childs)
            {
                PopulateChild(item);
            }
        }
        @foreach (var item in Model)
        {
            <tr>
                <td>@Html.DisplayFor(modelItem => item.id)</td>
                <td>@Html.DisplayFor(modelItem => item.name)</td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new {id = item.id}) |
                    @Html.ActionLink("Details", "Details", new {id = item.id}) |
                    @Html.ActionLink("Delete", "Delete", new {id = item.id})
                </td>
            </tr>

            foreach (var child in item.childs)
            {
                @PopulateChild(child)
            }

        }

关于c# - 在 Razor View 中遍历列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36643803/

相关文章:

javascript - 在每个 Ajax.BeginForm 上传递默认的 AjaxOption

ViewModel 中的 c# 空引用

c# - 为什么这些方法的圈复杂度如此之高?

c# - 在 C# 中使用 Excel InterOp 设置饼图样式

asp.net - System.Net.Cookie 和 System.Web.HttpCookie 有什么区别?

asp.net - 从signalr js客户端到集线器功能传递连接 token 是安全的或hacky

c# - 输出缓存不起作用

c# - 文件内容搜索 C#

asp.net - 如何使用 MSBuild 发布 Asp.NET Web 应用程序?

c# - 如何在 WebGrid 中禁用分页