Create Dynamic Menu in ASP.NET MVC – A Complete How to Guide

A menu plays a significant role in lending an amazing UX by making an application easily navigable. It can be used to make accessibility to a particular section a breeze.If you want to ensure a surefire application, it is essential that elements of your application must be accessed with ease. It must appear intuitive and intriguing, so that users can execute them with a flair. You may create multiple modules within a project and depending upon the user’s permissions, an appropriate menu can be implemented dynamically via ASP.NET.

Create Dynamic Menu in ASP.NET MVC - A Complete How to Guide

ASP.NET is an open source server side framework that augments web app development with great efficiency and precision. It is the core of the popular Content Management Systems (CRM), eCommerce, and so forth that deliver utile features.

Here is a comprehensive guide that offers a complete tutorial for creating a user-friendly dynamic menu via ASP.NET MVC. The process is extremely simple with only 5 easy steps at a glance.

Let’s distill the process and explore how dynamic menus can be created efficiently.

Create Dynamic Menu in ASP.NET MVC

Step 1: Create a database table

To create dynamic menus in ASP.NET MVC, the very first step is to generate a database table that can hold all the menu items in a designed hierarchy (if any). The database table can be created with a simple query as mentioned below.

 

This query will create a table Menus that will hold four values. These values are:

ID: is the primary key and auto generated
Parent ID: is the foreign key
Title: Name of the field
Description: is the information that you want to display when a user hovers over the menu.

With this, a desired table will be created in the database.

 

Step 2: Insert values into the table

A simple insert query can be used for adding the data into the table. Now, there are two possibilities, that is, your menu may possess multiple parent items that further possess child items or there may be parent items only. So, you must insert the values as per your app requirements.

Query to insert values into the table Menus.

 

By implementing the above mentioned queries, your Menus table will contain three items in the Menu and each item will further have a child item.

 

Step 3: Fetch the data from the table

Now, the next step is to fetch the menu items from the table Menus that was created in the step 1, and return a list of all the items.

For this, a GetMenus() function is created in MyMenu class. In this function, there is a loop that will help fetch the items of the Menu and return them via a list. The lines of code for the same is as follows.

Code Snippet for model:

Step 4: Display the created menu on the screen

Now, when the Menu items has been fetched from the database, it is the time to represent it in the view.

Code Snippet for View:

 

Here, ui and li HTML tags are used in the loop. This will help display the created menu in the view. Now, move to the next step.

 

Step 5: Beautify the menu to make it appear captivating

To ensure an enticing and easily readable menu, it is essential to make it appear visually appealing and intriguing. You may use the CSS appropriately to enhance the look and feel of the menu in a desired fashion. Here, I have incorporated the following chunk of code in the CSS file to boost the navigation ease and ensure an attractive Menu.

Code Snippet CSS:

 

This CSS has been created considering a few requirements, you may develop it in a desired fashion to bring substantial changes in the visual appearance. Therefore, you may streamline the CSS as per your UX and design needs.

 

Wrapping Up

By following this absolute guide, you can efficiently create beautiful dynamic menus via the ASP.NET MVC. It is advisable to thoroughly go through each step and design desirable menu while ensuring utmost navigation ease.

 

Author Bio
Sarah Parker is an experienced PSD to WordPress service provider, and a web designer. She loves to share her thoughts on web design and web development trends.

15 thoughts on “Create Dynamic Menu in ASP.NET MVC – A Complete How to Guide”

    1. Abinash and Shreekrishna are on crack… So you didn’t see the @foreach commands in the code? That IS mvc style code you tards… Just go back to digging ditches or manual labor… something your minds can comprehend…

  1. The concept is there but this code is filled with errors. Even the basic Model code is wrong. Am not sure what version of MVC you are using, maybe am too used to the newer versions

  2. This code is not working for me…lot of glitches in code…..even the explanation of the steps is missing too!!!

  3. This is anything but complete. However, it did make a good starting point to something I was able to follow thru to completion and I thank the author for that.

  4. It’s actually a great and helpful piece of information. I’m glad that you shared this useful information with us.
    Please keep us up to date like this. Thank you for sharing.

  5. Hi,

    I posted a comment this morning but I don’t see it in the “comments” section.
    Hence I am adding the question again:

    The code generates the Parent items but does not generate the children items.

    The following clist throws a null error ( no data )
    var clist = myMenu.Where(m => m.ParentID == pitem.ID).ToList();

    The database table does data, as follows:
    ID ParentID Title Description
    1 NULL Item One Desc
    2 NULL Item Two Desc
    3 NULL Item Three Desc
    4 1 Item Sub One Desc
    5 2 Item Sub Two Desc
    6 3 Item Sub Three Desc

    I’d really appreciate your help in resolving this issue.

    Thanks,
    Sunny.

  6. Thanks to Sarah Parker.
    i am developing web apps using newer and cross platform open source framework of asp.net core

    i ran into a problem generating menu from database table and i got benefit from this tutorials. although i didn’t include all the logic.

    logic i included
    1. The design of the database table with hierarchy
    2. Code snippet for view with for each iterating over returned model data.

    helpful.

  7. Good post., Thanks!

    In real world, we often need to display active menu based on content shown. Also, we need to expand the parent menu node. How to achieve this?

Leave a Comment

Your email address will not be published. Required fields are marked *