Hi ,
How to make the Dynamic menu from database in razor page appliication .Can any one suggest with sample code .
Thanks in advance ,
Karthik.K
Hi ,
How to make the Dynamic menu from database in razor page appliication .Can any one suggest with sample code .
Thanks in advance ,
Karthik.K
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Karthik KPosted May 23, 2025, 7:05 AM
@jithu Thomas ,
this is my _Layout.cshtml page :
@RenderBody()
This is my _PartialMenu.cshtml Page
@using DyMenu.Pages
@model DyMenu.Pages.IndexModel
This is my Index.cshtml.cs page .
public class IndexModel : PageModel
{
#region Declartion
private IMenuItemAccess _menuItemAccessDA;
private readonly IConfiguration _configuration;
#endregion
public IndexModel(IConfiguration configuration)
{
_configuration = configuration;
}
[BindProperty]
public IEnumerable
database : Menu
1, 0, 'Index', 'Index'
2, 0, 'Privacy', 'Privacy'
3, 0, 'Service', 'Service'
4, 0, 'Products', 'Products'
5, 0, 'Register', 'Register'
Other than index page not working . the following error thrown we i run . can anyone suggest with solution
Here Menu item is generated from index.cshtml.somewhere in this part made mistake. i am not sure how to pass the
menuitem model commonly
Error :
InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'DyMenu.Pages.ProductsModel',
but this ViewDataDictionary instance requires a model item of type 'DyMenu.Pages.IndexModel'.
Thanks in advance
Karthik.K
Jithu ThomasPosted Nov 2, 2023, 8:26 AM
Step 1
Create a model class that represents your menu items. This model class should map to your database table. Example as follows:
Step 2
Create a data access layer to fetch menu items from the database. You can use Entity Framework Core or any other data access technology you prefer.
Example by using Entity Framework Core:
Step 3
In your Razor Page (e.g., Index.cshtml), inject the MenuService and fetch the menu items. Then, recursively render the menu structure.
Step 4
Please retrieve the menu items using the MenuService in your Razor Page's Page Model (Index.cshtml.cs).
Step 5
Configruation of the services in Startup.cs
Note: Ensure you have your database context configured and your connection string set in appsettings.json.