-1

我有一个名为类别的列表。它包含以下数据。

Name  -                  Id 
----------------------------
Mobile  -                 0
Cellphones -             21
Mobile Accessories -     22
Camera -                  0
Camcorder -              55
Camera Accessories       60
SLRs                     61
Photo Printers           63
Computers                0
Laptops                  65
Memory Cards             67
Monitors                 69
RAM                      70
Computer Accessories     71

我想使用 Ul 和 Li 标签按以下方式显示上述数据:

移动的

-手机

-手机配件

相机

-摄像机

-相机配件

-单反

-照片打印机

电脑

-笔记本电脑

-记忆卡

-显示器

-内存

-电脑配件

即 id=0 表示父类,id=非零表示子类

帮助我。在此先感谢。

4

2 回答 2

0
Namespace Test

Public Class Class1

    Dim Categories As List(Of Category) = New List(Of Category) _
                                          From {New Category With {
                                                .ID = 0, .Name = "Mobile",
                                                .Items = New List(Of CategoryItem) _
                                                    From {New CategoryItem With {.ID = 1, .Name = "Cellphones"},
                                                          New CategoryItem With {.ID = 2, .Name = "Mobile Accessories"}
                                                         }
                                                },
                                                New Category With {
                                                .ID = 1, .Name = "Camera",
                                                .Items = New List(Of CategoryItem) _
                                                    From {New CategoryItem With {.ID = 1, .Name = "Camcorder"},
                                                          New CategoryItem With {.ID = 2, .Name = "Camera Accessories"},
                                                          New CategoryItem With {.ID = 3, .Name = "SLRs"},
                                                          New CategoryItem With {.ID = 4, .Name = "Photo Printers"}
                                                         }
                                                },
                                                New Category With {
                                                .ID = 2, .Name = "Computers",
                                                .Items = New List(Of CategoryItem) _
                                                    From {New CategoryItem With {.ID = 1, .Name = "Laptops"},
                                                          New CategoryItem With {.ID = 2, .Name = "Memory Cards"},
                                                          New CategoryItem With {.ID = 3, .Name = "Monitors"},
                                                          New CategoryItem With {.ID = 4, .Name = "RAM"},
                                                          New CategoryItem With {.ID = 5, .Name = "Computer Accessories"}
                                                         }
                                                }
                                                }

    Public Sub New()
     Dim ul As String =  SetCategories()
    End Sub

    Private Function SetCategories() As String
        Dim ulCategories As XElement = <ul class="float-left"></ul>

        For Each cat As Category In Categories
            Dim currentCategory As Category = cat
            Dim liCategory As XElement = <li><%= currentCategory.Name %></li>
            Dim ulCategoryItems As XElement = <ul></ul>
            For Each item As CategoryItem In currentCategory.Items
                Dim currentItem As CategoryItem = item
                Dim liItem As XElement = <li><%= currentItem.Name %></li>
                ulCategoryItems.Add(liItem)
            Next item
            liCategory.Add(ulCategoryItems)
            ulCategories.Add(liCategory)
        Next cat

        Return ulCategories.ToString()

    End Function

End Class

Public Class Category

    Public Property ID As Integer
    Public Property Name As String
    Public Property Items As List(Of CategoryItem)

End Class

Public Class CategoryItem
    Public Property ID As Integer
    Public Property Name As String
End Class

End Namespace
于 2012-11-22T02:30:17.410 回答
0

在这里,把它放在你的视野中:

<ul>
    <li>Mobile
      <ul>
          <li>Cellphones</li>
          <li>Mobile Accessories</li>
      </ul>
    </li>    
    <li>Camera
      <ul>
          <li>Camcorder</li>
          <li>Camera Accessories</li>
          <li>SLRs</li>
          <li>Photo Printers</li>
      </ul>
    </li>    
    <li>Computers
      <ul>
          <li>Laptops</li>
          <li>Memory Cards</li>
          <li>Monitors</li>
          <li>RAM</li>
          <li>Computer Accessories</li>
      </ul>
    </li>    
</ul>

虽然这就是你所要求的,但我认为你想要更多的东西。

由于您的问题或您的任何尝试没有任何具体内容,因此在以下解决方案中做出了几个假设。

班级:

public class Category
{
  public int Id { get; set; }
  public string Name { get; set; }
  public List<Category> Subcateogries { get; set; }

  public Category()
  {
    Subcateogries = new List<Category>();
  }
}

控制器:

public ActionResult Index()
{
  //The Data
  var categories = new List<Category>
  {
    new Category { Id = 0, Name = "Mobile" },
    new Category { Id = 21, Name = "Cellphones" },
    new Category { Id = 22, Name = "Mobile Accessories" },
    new Category { Id = 0, Name = "Camera" },
    new Category { Id = 55, Name = "Camcorder" },
    new Category { Id = 60, Name = "Camera Accessories" },
    new Category { Id = 61, Name = "SLRs" },
    new Category { Id = 63, Name = "Photo Printers" },
    new Category { Id = 0, Name = "Computers" },
    new Category { Id = 65, Name = "Laptops" },
    new Category { Id = 67, Name = "Memory Cards" },
    new Category { Id = 69, Name = "Monitors" },
    new Category { Id = 70, Name = "RAM" },
    new Category { Id = 71, Name = "Computer Accessories" }
  };

  var GroupedData = new List<Category>();

  foreach(var category in categories)
  {
    if(category.Id == 0)
    {
      GroupedData.Add(category);
    }
    else
    {
      GroupedData.Last().Subcateogries.Add(category);
    }
  }

  return View(GroupedData);
}

看法:

@model List<Category>

<h2>Categories</h2>

<ul>
  @foreach (var topCategory in Model)
  {
    <li>@topCategory.Name
      <ul>
        @foreach (var subCategory in topCategory.Subcateogries)
        {
          <li>@subCategory.Name</li>
        }
      </ul>
    </li>    
  }
</ul>

这是假设起始列表(在这种情况下为类别)是您想要的顺序。在创建GroupedData时,我还通过重用原始列表项来走捷径。还假设永远只有两个级别。

可以重构视图以利用部分视图并处理多个级别,但这不是问题的一部分。

于 2012-11-22T03:23:22.863 回答