I have a table called Categories and it contains the following fields:
Id | Name | ParentId //parent id actually is a self reference
so from the _Layout.cshtml am calling the partial view called MainMenu.cshtml
_Layout.cshtml
@Html.Partial("MainMenu")
How can I can send the model to the mainmenu, i know there is an overload of partial, where i can send a model as well. am trying to solve the problem in an elegant way that doesn’t break the MVC architecture.
I guess that the menu isn’t different in each page, so why call partial inside every view and pass the MenuModel to every view?
Instead, use
@Html.Action("Menu").Have an action that retrieves the menu items from the database.
using entity framework, you can have the following class in your database:
You retrieve the class, then pass it to an HtmlHelper extension method or simply a helper function in the cshtml file that retrieves the MenuModel object and display it.
The logic is simple by using Recursive Function:
print current item. if has children -> create a table/container for the inner items and call inside the container to the same function for every child function.