Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Scott Ritshie 35 posts 100 karma points
    Jun 19, 2017 @ 13:47
    Scott Ritshie
    0

    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

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jun 19, 2017 @ 13:49
    Alex Skrypnyk
    0

    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

  • Scott Ritshie 35 posts 100 karma points
    Jun 19, 2017 @ 14:09
    Scott Ritshie
    0

    Hi Alex,

    I had tried this already and couldn't get ViewData["Phone"] to display on any page.

    CustomStartup.cs:

    namespace MySite.App_Start
    {
      public class CustomStartup : ApplicationEventHandler
      {
        protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
        {
          DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(MySite.Controllers.MasterController));
        }
      }
    }
    

    MasterContoller.cs:

    namespace MySite.Controllers
    {
      public class MasterController : RenderMvcController
      {
        public ActionResult MasterMaster(RenderModel model)
        {
          var settingsNode = Umbraco.Content(1065);
          string phone = settingsNode.GetPropertyValue("PhoneNumber");
          ViewData["Phone"] = phone;
    
          return base.Index(model);
        }
      }
    }
    

    Master.cshtml:

    @ViewData["Phone"]
    

    Any thoughts on what I may have missed?

    Thanks! Scott

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jun 19, 2017 @ 16:30
    Alex Skrypnyk
    0

    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:

        var settingsNode = Umbraco.TypedContent(1065);
        string phone = settingsNode.GetPropertyValue("PhoneNumber");
    

    You can cache views, cache pages, cache macros, it's not performance expensive operation.

    Thanks,

    Alex

  • Scott Ritshie 35 posts 100 karma points
    Jun 19, 2017 @ 17:17
    Scott Ritshie
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft