I am trying to populate the nested Ordered list using @functions & @helper features in ASP.NET MVC Razor.

I am successful in creating nested list using @functions, but when I tried to the same with @helper method execution is not going inside the helper method.

Model:

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

View Model:

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

Controller:

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>

When a recursive call to the helper method is made execution is going to the method, but the execution is skipped.

share|improve this question

just add "@" in front of the method when recursive call.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.