Quote:
in this case
In which case?
Dynamic menus can be created easily, you can use a query that extracts all the menu records from the database table, and then generate a menu based on that. For example, here is a sample table,
| MenuId | MenuPlacing |
|--------|-------------|
| 1 | Navigation |
| 2 | Footer |
| 3 | Post |
You can see that instead of creating menus, I created a list that will help me select the type of menu I want. Now this can be used to further add the menu items,
| MenuItemId | MenuId | Heading | Tooltip | Hyperlink | Active |
|------------|--------|---------|-------------|------------|--------|
| 1 | 1 | Home | Home page | "/" | true |
| 2 | 1 | About | Our company | "/about" | true |
| 3 | 1 | Contact | Reach us | "/contact" | true |
| 4 | 1 | Sales | Sales team | "/sales" | true |
These kind of tables can help you use the search query in SQL and fetch the records for a particular menu. Then you can render the data on the HTML using
foreach
statements, suppose after the response you have a variable that holds the output of the MenuItem table. You can iterate over the result and render a list,
<ul>
@foreach (var menuItem in menuItemList) {
if(menuItem.Active) {
<li>
<a href="@menuItem.Hyperlink" title="@menuItem.Tooltip">
@menuItem.Heading
</a>
</li>
}
}
</ul>
This will setup a list of the items, that you selected from your database.
You can check my article on SQL Server connections with C# language,
How to connect SQL Database to your C# program, beginner's tutorial[
^].