There are a lot of similar questions, but no good answers. This is my attempt:
I want to use a partial view in _Layout.cshtml for a menu that will appear on every page.
My _Layout.cshtml:
<div id="navbar" class="navbar-collapse">
<partial name="_TopLevelMenu" model="Model.MainMenuModel" />
</div>
My _TopLevelMenu.cshtml partial:
@model List<Data.Models.MainMenuModel>
<ul id="ulMainMenu" class="nav navbar-nav">
@foreach (var item in Model){
<li @item.NavId>
<a href="@item.NavigationUrl">@item.DisplayName</a>
</li>
}
</ul>
My error:
RuntimeBinderException: 'MyApp.Pages.IndexModel' does not contain a definition for 'MainMenuModel'
Things I've tried:
"Pass the model" - I get the same error when I do this, and besides, Visual Studio strongly advises me to use the Partial tag helper instead.
Use the partial
tag without a named model, which gives me this error: InvalidOperationException: The model item passed into the ViewDataDictionary is of type 'MyApp.Pages.IndexModel', but this ViewDataDictionary instance requires a model item of type 'System.Collections.Generic.List
1[MyApp.Data.Models.MainMenuModel]'.`
I tried it with just MainMenuModel
but Visual Studio tells me it's a type and I can't use a type for my model.
I looked at this website for guidance, and it unhelpfully uses some of my key search words ("menu", "layout") but doesn't use the _Layout.cshtml page as part of the example.
If I do <partial name="_TopLevelMenu" model=new MainMenuModel()" />
I don't get errors but I also don't get my data from the partial to populate.
What must I do to properly have a partial be the menu items on all pages?