Reputation: 1237
I'm trying to use _layouts for a shop site.
Every page has different data with only the left menu data being common. It seems I'm being forced to have a gigantic global model with every possible bit of data represented in it. e.g ShopModel.menu,ShopModel.category,ShopModel.footer,ShopModel.category leader etc. It just goes on forever and gets very deep an complicated.
Is there no way to separate the code so that the menu handles the menu and the category handles the category etc. Seems like a big backward step from Web forms. I tried a _partial with its own @model for the menu but it seems its ignored and the CHTML is still looking for data in the main ShopModel.
Of course on day one I tried adding multiple @models to the page but that doesn't work.
Edit: Unfortunately, I must not explain it very well as I would see this as a first-day problem. I would expect the _layout to contain the MENU @model as its common. I would then expect to add my CATEGORY @model to the index page as it loops Categories in a foreach.
The Html is fine. I know how to add a partial but how do I add a self-sufficient partial with dynamic foreach data for example.?
Upvotes: 1
Views: 85
Reputation: 1237
ViewComponents seem to be the answer to keep thing neat and separate.
Upvotes: 1
Reputation: 867
If the left menu is a common element for the whole system, just put its code in the Layout.cshtml page in the Shared folder. All the visual elements that are common to the whole system can be put there, the Header, this menu, the footer, etc. The _ViewStart.cshtml page at the root of the View folder sets the system to look for the page referenced in its Layout
parameter and involve every View it has within it. Partial Views just mean rendering/coding the page body without including this common information.
Upvotes: 0