Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1055181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:31:25+00:00 2026-05-16T17:31:25+00:00

I have a ViewModel: public class Page { public int Id { get; set;

  • 0

I have a ViewModel:

public class Page
{
    public int Id { get; set; }
    public Page Parent { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
}

I am passing that model to a view, and i need to create a hierarchical menu based on the model:

<ul>
    <li>Page
        <ul>
            <li>Sub Page</li>
        </ul>
    </li>

Through no lack of effort at all i am unable to figure out how i will be doing this recursive loop in the view.

I can create the markup in my controller, but that’s not test friendly.

Any Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T17:31:26+00:00Added an answer on May 16, 2026 at 5:31 pm

    there are many ways to display dynamicly generated menus and i will post my way of doing it, I have two classes Menu & MenuItem (a Menu is just a wrapper for a list of MenuItems which are the actual >”real” links) and then i have a NavigationViewModel to wrap everything.

    public class MenuItem
    {
        public int MenuID { get; set; }
        public int ID { get; set; }
        public String Label { get; set; }
        public String Link { get; set; }
        public Boolean Show { get; set; }
    
        public MenuItem(int menuId, int id, string label, string link, Boolean show)
        {
            this.MenuID = menuId;
            this.ID = id;
            this.Label = label;
            this.Link = link;
            this.Show = show;
        }
    }
    

    //

    public class Menu
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<MenuItem> MenuItems { get; set; }
    
        public Menu(int id, string name)
        {
            this.ID = id;
            this.Name = name;
            this.MenuItems = new List<MenuItem>();
        }
    
    }
    

    // this is NavigationModel that wrap everything.

    public class NavigationModel
    {
        public int currentMenuID { get; set; }   
        // used to determine the current displayed Menu to add
        // the "current" class to it. (to be set in the controller)
    
        public int currentMenuItemID { get; set; } 
        // used to determine the current displayed MenuItem to add
        // the "current" class to it. (to be set in the controller)
    
        public List<Menu> Menus { get; set; }
    
    
        public NavigationModel()
        {
            this.Menus = new List<Menu>();
            // Set Default Menu ( Menu 1 )
            this.currentMenuID = 1;
            // Set Default Menau Item ( None )
            this.currentMenuItemID = 0;
        }
    }
    

    // This is an example of a method that builds a NavigationModel.

    private static NavigationModel BuildNavigationMenu(User currentUser, string rootURL)
        {
            string loginURL = rootURL + "Account/LogOn";
    
            // Main Menu
            Menu MainMenu = new Menu(1, "Home");
            MainMenu.MenuItems.Add(new MenuItem(1, 1, "Welcome", rootURL, true));
            MainMenu.MenuItems.Add(new MenuItem(1, 2, "How It Works", rootURL + "Home/HowDoesItWork", true));
    
            // Work Menu
            Menu WorkMenu = new Menu(2, "Work");
            WorkMenu.MenuItems.Add(new MenuItem(2, 1, "Profile", rootURL + "User/Profile/" + currentUser.ID , true));
            WorkMenu.MenuItems.Add(new MenuItem(2, 2, "Customers", "#", true));
    
            // Add Menus To Navigation Model
            NavigationModel navigationMenu = new NavigationModel();
            navigationMenu.Menus.Add(MainMenu);
            navigationMenu.Menus.Add(HireMenu);
    
            return navigationMenu;
    }
    

    // I have a NavigationHelper that takes care of outputting the HTML with the following two methods.

    Note: (this is a simplified version as in my implementation i use the id of the Menu & the MenuItem to locate the current menu that is being displayed and add the “current” CSS class to it”

    public static string DisplayMenu(this HtmlHelper helper, NavigationModel navigationMenu)
    { 
        public static string DisplayMenu(this HtmlHelper helper, NavigationModel navigationMenu)
        {            
            String result = "<ul id='main-nav'>\n";
            foreach(Menu menu in navigationMenu.Menus)
            {
                result +=     "<li>\n";
                result +=         string.Format("<a href='#'> {0} </a>\n",helper.AttributeEncode(menu.Name));
                result +=         "<ul>\n";
                foreach(MenuItem item in menu.MenuItems)
                {
                    result += NavigationHelper.ConvertToItem(helper, item);
                }
                result +=         "</ul>\n";
                result +=     "</li>\n";
            }
            result +=     "</ul>\n";
    
            return result;
        }
    
        private static string ConvertToItem(this HtmlHelper helper,MenuItem item)
        {
            if (item.Show)
            {
                return string.Format("<li><a href='{0}'>{1}</a></li>\n", helper.AttributeEncode(item.Link), helper.AttributeEncode(item.Label));
            }
            else { return ""; }   
        }
    }
    

    finally i have a following call in my Master to display the menu.

    <!-- Navigation -->
    <%= Html.DisplayMenu(Model.NavigationMenu) %>
    

    Note: I have a strongly typed MasterPage with the type (BaseViewModel) that contains the property NavigationMenu of type NavigationModel

    Connecting everything together.

    In my example i have to supply my Views with a ViewModel (that inherits my BaseViewModel) at each action method to and then build the Menu (using a Builder Method that does most of the work so i don’t have to re type it in every action method). If you don’t use a BaseViewModel then you will need to find another way to build the Navigation Model and then pass the Navigation Model to your MasterPage.

    For me i find having a strongly typed MasterPage to make things easier and cleaner. King wilder have a nice way of implementing them in his GolfTracker Series.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.