Hi All ,
How to create Populate Menu from in razor by creating new menu page instead of _layout.cshtml Page .
with detailed code
Thanks in advance ,
Karthik.K
Hi All ,
How to create Populate Menu from in razor by creating new menu page instead of _layout.cshtml Page .
with detailed 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.
Darshan AdakanePosted Jan 6, 2026, 10:55 AM
Hi Karthik
You can move the menu into a separate reusable component and include it in the layout.
A Partial View is ideal for menus because it keeps layout clean and allows reuse.
Step 1: Create a menu partial view
Create a file:
Step 2: Render menu in
_Layout.cshtmlThis keeps
_Layout.cshtmlclean and focused only on layout structure.Hope this helps.
Tasadduq BurneyPosted Dec 8, 2025, 5:49 PM
Hey,
Hope you're keeping well.
If you want to move menu rendering out of
_Layout.cshtml, create a strongly typed partial view (e.g.,_Menu.cshtml) that accepts aListmodel and iterate over it with Razor. Populate that model in your page’sOnGet(Razor Pages) or controller action (MVC) by loading menu data from a database via EF Core or from configuration, then pass it to the partial using@await Html.PartialAsync("_Menu", Model.MenuItems). This keeps menu logic isolated, makes it reusable, and avoids hardcoding links in layout. If the menu is dynamic and used across pages, consider injecting it via aViewComponentinstead, as it allows encapsulated rendering and dependency injection for data retrieval.Thanks and regards,
Taz