2016-04-15 8 views
3

Ich habe eine C# list mit mehreren Ebenen, wie folgt aus:durch Liste innerhalb Razor Iterieren Ansicht

enter image description here

Was will ich in meiner Meinung nach tun, ist in der Liste alle Elemente anzuzeigen, und nicht nur die Eltern. Mein Ergebnis mit meinem aktuellen Code ist:

enter image description here

Also, das sind die Eltern sind nur.

Meine Ansicht:

@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> 

Mein Modell:

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 
    } 

} 

Mein 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); 
     } 
    } 
} 

Ich hoffe, dass ich meine p gemacht Problem gelöst und jemand hat eine Lösung, danke im Voraus!

+1

Aufruf Wenn Sie alle untergeordneten Elemente angezeigt werden soll, dann können Sie die Childs Eigenschaft rekursiv durchlaufen. –

+1

Ihr Modell scheint nicht gut zu sein, Sie haben 'List ' sowohl in der 'Child'-Klasse als auch in der' RouteObject'-Klasse eingeschlossen, während es nur in der 'RouteObject'-Klasse sein sollte. – Mairaj

+0

@MairajAhmad Hmm, warum sollte das sein? Meine Kinder haben auch Kinder. Also ich denke meine Liste hat drei Ebenen. – Peurr

Antwort

5

können Sie tun, dass Inline-Funktion durch das Schreiben und es rekursiv

 @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) 
      } 

     } 
+1

Danke für diese großartige Antwort! – Peurr

Verwandte Themen