使用子类别循环类别

时间:2010-07-05 16:11:14

标签: asp.net-mvc view foreach typed

1)我在db上有2个选项卡类别和子类别,关系为1到多(使用实体框架)

2)我必须创建一个像这样的垂直菜单

<ul>
   <li>category 1
    <ul>
       <li>subcategory 1</li>
       <li>subcategory 2</li>      
       <li>subcategory 3</li>
    </ul>
   </li>
</ul>

我认为我的问题在于我的功能

 Function List_category_subcategory() As List(Of WHAT HERE???????)

        Using db As New DBTestEntities

            Dim q = From cat In db.categories Join subcat In db.subcategories On cat.CategoryID Equals subcat.CategoryID _
            Select New With {cat.CategoryName, subcat.SubCategoryName}

        List_category_subcategory = WHAT HERE???????

        End Using

    End Function

因为我不知道必须返回什么函数(可能是列表集合)

在我看来之后必须循环一切

类似的东西

<ul> 
<%  For Each cat In ??????%>
 <li><a href="#"><%=Html.Encode(cat.CategoryName)%></a>
            <ul>
            <% For Each subcat In ???????%>          
            <li><%=Html.Encode(subcat.SubCategoryName)%></li>
            <% Next%>
            </ul>
</li>
<% Next%>
</ul>

2 个答案:

答案 0 :(得分:1)

SubCategory不应该是category的属性(这似乎更自然,但我可能在这里错了)?此外,嵌套循环中存在格式错误的li标记(<li<%=):

<ul id="menu-1" class="menu"> 
    <% For Each category In Model.Category %>
    <li>
        <a href="#"><%= Html.Encode(category.CategoryName) %></a>
        <ul>
        <% For Each subcategory In category.SubCategory %>          
            <li>
                <%= Html.Encode(subcategory.SubCategoryName) %>
            </li>
        <% Next %>
        </ul>
    </li>
    <% Next %>
</ul>

答案 1 :(得分:0)

缺少关闭&gt;在内心?