Content from single controller that renders in all templates?
Hello,
I have a Master.cshtml that is the base template for the site. I also have a doc type called Master. I can push content from "HomeController" to ActionResult Home (Home.cshtml), but I'd like to populate global info on all pages using a single (perhaps default) controller that all templates read from.
I tried putting the logic into MasterController, ActionResult Master, then setting ViewData["Phone"] so that Master.cshtml could read it, but got nothing. It did work, as I said above, when creating a HomeController, but only the Home page got the data.
How would I go about using a single controller that Master.cshtml could display data on every page, regardless of doc type of template?
I want to avoid copying the same code to every template page that will display the same data. That's why I'm looking for a default controller type of operation that every page in the site (mostly the Master page) can access via ViewData.
Content from single controller that renders in all templates?
Hello,
I have a Master.cshtml that is the base template for the site. I also have a doc type called Master. I can push content from "HomeController" to ActionResult Home (Home.cshtml), but I'd like to populate global info on all pages using a single (perhaps default) controller that all templates read from.
I tried putting the logic into MasterController, ActionResult Master, then setting ViewData["Phone"] so that Master.cshtml could read it, but got nothing. It did work, as I said above, when creating a HomeController, but only the Home page got the data.
How would I go about using a single controller that Master.cshtml could display data on every page, regardless of doc type of template?
Thanks, Scott
Hi Scott
It looks like you have to use the default controller, read more how to do it:
https://our.umbraco.org/documentation/reference/routing/custom-controllers#change-the-default-controller
Thanks,
Alex
Hi Alex,
I had tried this already and couldn't get ViewData["Phone"] to display on any page.
CustomStartup.cs:
MasterContoller.cs:
Master.cshtml:
Any thoughts on what I may have missed?
Thanks! Scott
Hi Scott
What is an advantage of saving all homepage data to the ViewData?
You can access homepage data with whenever you want with this code:
You can cache views, cache pages, cache macros, it's not performance expensive operation.
Thanks,
Alex
I want to avoid copying the same code to every template page that will display the same data. That's why I'm looking for a default controller type of operation that every page in the site (mostly the Master page) can access via ViewData.
is working on a reply...