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.
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
Tasadduq BurneyRank #1,427 · 1.1K reputationPosted Dec 8, 2025, 12:49 PM EST
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
Darshan AdakaneRank #344 · 7.3K reputationPosted Jan 6, 2026, 5:55 AM EST
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.