Hi All ,
How to create dynamic menu in Razor page in asp.net core.
#1. Menu from mysql database .
#2. with multilevel menu
Thanks in advance ,
Karthik.K
Hi All ,
How to create dynamic menu in Razor page in asp.net core.
#1. Menu from mysql database .
#2. with multilevel menu
Thanks in advance ,
Karthik.K
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Eliana BlakeRank #1,595 · 993 reputationPosted May 9, 2025, 3:23 AM EDT
Hello Karthik.K,
Creating a dynamic menu in Razor pages in ASP.NET Core can greatly enhance the user experience of your web application. To achieve this, you can follow these steps:
1. Menu from MySQL Database:
- First, you need to fetch the menu items from your MySQL database. You can do this by writing a service or repository that interacts with your database.
- In your Razor page, you can iterate over these menu items and dynamically generate the menu using HTML elements like `
` and `- `.
C#
- Here's a simple example of how you can populate a menu from a MySQL database in ASP.NET Core:
@foreach (var menuItem in Model.MenuItems) { @menuItem.Name }2. Multilevel Menu:
- To create a multilevel menu, you can incorporate a recursive function to handle nested menu items. Each menu item can have a list of child items, allowing you to create submenus.
- You can utilize HTML structure like nested `
` and `- ` elements to represent the hierarchy of your multilevel menu.
C#
- Here's a basic example of a recursive function in C# for rendering a multilevel menu:
void RenderMenu(List items) { @foreach (var item in items) { @item.Name @if (item.Children.Any()) { RenderMenu(item.Children); } } }By following these steps, you can create a dynamic menu in Razor pages in ASP.NET Core that pulls data from a MySQL database and supports multilevel menu structures. Remember to adapt the code snippets to fit your specific requirements and data models.
If you have any further questions or need more specific guidance, feel free to ask!
Best regards,
[Your Name]