1

我有一张桌子像:

MenuTreeID      ParentID   MenuName
----------      --------   --------
1                0         Menu No 1
2                1         Menu No 1.1
3                1         Menu No 1.2
4                3         Menu No 1.2.1
5                3         Menu No 1.2.2
6                0         Menu No 2
7                6         Menu No 2.1
8                6         Menu No 2.2
9                1         Menu No 1.3
... ...   ...  ... 

现在我需要以下 Unorderlist :

Menu No 1
     Menu No 1.1
     Menu No 1.2
          Menu No 1.2.1
          Menu No 1.2.2
     Menu No 1.3
Menu No 2
     Menu No 2.1
     Menu No 2.2

... ... ... ... 

如何使用 MVC Razor 视图引擎编码(cshtml 编码)获得它?我尝试以下代码:

<ul>   
@foreach (ESimSolMVC05.Models.MenuTree oItem in Model)
             {
                 <li>                
                    @oItem.MenuName
                    @if (oItem.SubItems.Count > 0)
                    {
                        @ShowSubItems(oItem.SubItems)
                    }
                 </li>
             }
</ul>
@helper ShowSubItems(ESimSolMVC05.Models.MenuTrees oSubItems)
    {
     <ul>
         @if (oSubItems.Count > 0)
         {
             foreach (ESimSolMVC05.Models.MenuTree oTempItem in oSubItems)
             {
                 <li>                 
                    @oTempItem.MenuName
                    @if (oTempItem.SubItems.Count>0)
                    {
                        @ShowSubItems(oTempItem.SubItems)
                    }
                 </li>
             }
         }
     </ul>
 }

这里的子项意味着当我创建我的集合时,我通过 (Select * From Menutree Where ParentID = MenuTreeID) 查询获得子项。我的输出 UnOrder List 是:

**Menu No 1
     Menu No 1.1
     Menu No 1.2
Menu No 1.1   (Duplicate)
Menu No 1.2   (Duplicate)

Menu No 2
     Menu No 2.1
     Menu No 2.2
Menu No 1.2.1   (Order not maintain)
Menu No 1.2.2   (Order not maintain)
Menu No 1.3     (Order not maintain)**
4

0 回答 0