'MVC 5 Use a shared header and footer layout across a site that loads different models on each page
I am trying to create a universal layout across my site. The layout is made up of the header and footer from 2 separate files. I have a file for the header called _LayoutHeader.cshtml and a file for the footer called _LayoutFooter.cshtml. I want all the pages to use this layout so that I do not continue to have repetitive code like writing the navigation menu and scripts that are in the head section and footer sections over and over on each page.
I load the header on all my pages by using the following at the top of the pages.
@RenderPage("~/Views/Shared/_Partials_and_Views/_LayoutHeader.cshtml")
The footer is loaded like this by me calling it at the bottom of all the pages.
@RenderPage("~/Views/Shared/_Partials_and_Views/_LayoutFooter.cshtml")
The _LayoutHeader.cshtml file contains the dropdown navigation with all the required links and all the CSS references, bootstrap references, scripts for APis that are involved as well as valuable SEO tags like description, title, index follow and no follow etc....
The _LayoutFooter.cshtml file contains more links that also navigate to different pages depending on where in the app you are clicking it from. If my client A clicks on his link called Files, he will see different content from my Client B and Client C that also click on their links called Files. So I use models to handle these clicks on those links.
These 2 files also include the call to a model that is used across the site. @model MultiCMS.Models.tblClient .
There are some pages though that use a different model like this one: @model MultiCMS.Models.tblProducts
How am I able to use that layout (the header and the footer combination mentioned above) on pages that call a different model on? By "model" I mean I am using entity framework and in order for me to load the data that I need on each page. I call these models at the top of the page to grab and display the data.
The issue I am having is that when I am trying to use that combo of the 2 layout files, its failing on the pages that are not using @model ClientsApp.Models.tblClient. Like in the case of the pages that use @model MultiCMS.Models.tblProducts it fails but works on other pages that use @model MultiCMS.Models.tblClient .
Error:
The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.tblProducts_A96074721A46B7C99112D6FEA49F0762848C8', but this dictionary requires a model item of type 'MultiCMSApp.Models.tblClient'. If I want to use the same layout on all the pages but some have different models than the layout pages what do I do ?
This is a web application built on asp.net MVC 5.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|