2015-12-16 72 views
0

我想在ASP.NET MVC Razor中使用@functions & @helper功能填充嵌套的Ordered列表。遞歸調用ASP.NET MVC中的@helper方法Razor,@helper方法中的代碼在執行期間被跳過

我成功地使用@functions創建嵌套列表,但是當我嘗試使用@helper方法執行時,執行不在輔助方法內部。

型號:

public class NavigationMenuModels 
    { 
     public int ID { get; set; } 
     public int? ParentID { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 

     public List<NavigationMenuModels> SubNavigationMenu { get; set; } 
    } 

視圖模型:

public class NavigationMenuViewModel 
    { 
     public NavigationMenuViewModel() 
     { 
      ListMenu = new List<NavigationMenuModels>(); 
     } 
     public string ListName { get; set; } 
     public List<NavigationMenuModels> ListMenu { get; set; } 
    } 

控制器:

public ActionResult NavigationMenu() 
     { 
      //Menu 
      NavigationMenuModels objMenu = new NavigationMenuModels() { ID = 1, ParentID = null, Name = "Menu", Description = "Menu" }; 
      //Menu Items 
      List<NavigationMenuModels> objMenuItems = new List<NavigationMenuModels>(); 
      objMenuItems.Add(new NavigationMenuModels() { ID = 1, ParentID = 1, Name = "Home", Description = "Home" }); 
      objMenuItems.Add(new NavigationMenuModels() { ID = 2, ParentID = 1, Name = "About", Description = "About" }); 
      objMenuItems.Add(new NavigationMenuModels() { ID = 3, ParentID = 1, Name = "Help", Description = "Help" }); 
      objMenuItems.Add(new NavigationMenuModels() { ID = 4, ParentID = 1, Name = "Contact", Description = "Contact" }); 
      objMenu.SubNavigationMenu = objMenuItems; 

      //Admin 
      NavigationMenuModels objAdmin = new NavigationMenuModels() { ID = 2, ParentID = null, Name = "Admin", Description = "Admin" }; 
      //Admin Items 
      List<NavigationMenuModels> objAdminItems = new List<NavigationMenuModels>(); 
      objAdminItems.Add(new NavigationMenuModels() { ID = 1, ParentID=2, Name = "User Permissions", Description = "User Permissions" }); 
      objAdminItems.Add(new NavigationMenuModels() { ID = 2, ParentID=2, Name = "Security", Description = "Security" }); 
      objAdmin.SubNavigationMenu = objAdminItems; 

      //Account 
      NavigationMenuModels objAccount = new NavigationMenuModels() { ID = 3, ParentID = null, Name = "Account", Description = "Account" }; 
      //Account Items 
      List<NavigationMenuModels> objAccountItems = new List<NavigationMenuModels>(); 
      objAccountItems = null; 
      objAccount.SubNavigationMenu = objAccountItems; 

      NavigationMenuViewModel objNavigationMenu = new NavigationMenuViewModel(); 
      objNavigationMenu.ListName = "Master Navigation"; 
      objNavigationMenu.ListMenu.Add(objMenu); 
      objNavigationMenu.ListMenu.Add(objAdmin); 
      objNavigationMenu.ListMenu.Add(objAccount); 

      return View(objNavigationMenu); 
     } 

CSHTML:

@using LearnAngularJs_App1.Models 
@using System.Text 
@model LearnAngularJs_App1.Models.NavigationMenuViewModel 

@{ 
    ViewBag.Title = "NavigationMenu"; 
} 

@functions 
{ 
     public static HtmlString GetNestedListHtml(NavigationMenuViewModel Crudeinput) 
     { 
      StringBuilder sb = new StringBuilder(); 
      var orderedList = new TagBuilder("ol"); 

      foreach (NavigationMenuModels NavMenu in Crudeinput.ListMenu) 
      { 
       var listItem = new TagBuilder("li"); 
       listItem.SetInnerText(NavMenu.Name); 
       sb.AppendLine(listItem.ToString(TagRenderMode.Normal)); 
       if (NavMenu.SubNavigationMenu != null) 
       { 
        if (NavMenu.SubNavigationMenu.Count > 0) 
        { 
         sb.AppendLine(BuildNestedList(NavMenu.SubNavigationMenu)); 
        } 
       } 
      } 

      orderedList.InnerHtml = sb.ToString(); 
      return new HtmlString(orderedList.ToString(TagRenderMode.Normal)); 
     } 

     public static string BuildNestedList(List<NavigationMenuModels> SubMenuList) 
     { 
      var sb = new StringBuilder(); 
      var orderedList = new TagBuilder("ol"); 

      if (SubMenuList.Count > 0) 
      { 
       foreach (NavigationMenuModels SubNavgationMenuitem in SubMenuList) 
       { 
        var listItem = new TagBuilder("li"); 
        listItem.SetInnerText(SubNavgationMenuitem.Name); 
        sb.AppendLine(listItem.ToString(TagRenderMode.Normal)); 

        if (SubNavgationMenuitem.SubNavigationMenu != null) 
        { 
         if (SubNavgationMenuitem.SubNavigationMenu.Count > 0) 
         { 
          sb.AppendLine(BuildNestedList(SubNavgationMenuitem.SubNavigationMenu)); 
         } 
        } 
       } 
      } 

      orderedList.InnerHtml = sb.ToString(); 
      return orderedList.ToString(TagRenderMode.Normal); 
     } 
} 

@helper BuildNestedListHelper(List<NavigationMenuModels> Crudeinput) 
{ 
    if (Crudeinput.Any()) 
    { 
     <ol> 
      @foreach (NavigationMenuModels NavMenu in Crudeinput) 
      { 
       <li> 
        @NavMenu.Name 
        @if (NavMenu.SubNavigationMenu != null) 
        { 
         BuildNestedListHelper(NavMenu.SubNavigationMenu); 
        } 
       </li> 
      } 
     </ol> 
    } 
} 

<h2>NavigationMenu</h2> 

<div> 
    <div><span>Bind Navigation Menu using razor @@funtions</span></div> 
    <div> 
     @GetNestedListHtml(Model) 
    </div> 
</div> 
<div> 
    <div><span>Bind Navigation Menu using razor @@helper</span></div> 
    <div> 
     @BuildNestedListHelper(Model.ListMenu) 
    </div> 
</div> 

當對輔助方法進行遞歸調用時,執行的是執行方法,但執行被跳過。

回答

2

遞歸調用時,只需在方法前添加「@」即可。

相關問題