Copied to clipboard

Flag this post as spam?

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


  • Greg Miller 9 posts 79 karma points
    Sep 21, 2016 @ 18:48
    Greg Miller
    0

    how to add external data to a master template

    Apologies for the most likely very newbie question, but hey I'm a newbie to this CMS world.

    I have an external database from which I would like to include some data from into my master template.

    Anyone have any suggestions on how to accomplish this within the Umbraco framework? Macros, partial views, surface controller, all of the above, none of the above :)

    thanks in advance

  • Biagio Paruolo 1594 posts 1825 karma points c-trib
    Sep 21, 2016 @ 20:23
    Biagio Paruolo
    0

    Hi,

    you need to create a normal connection string into the web.config and so use the .NET libraries to access data and display them.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 21, 2016 @ 20:28
    Alex Skrypnyk
    0

    Hi Greg,

    If you want to include some data to all pages, than you have to use master template and partial view maybe.

    If you need to insert some data only to one documentType - than you can use RenderMvcController, read more - https://our.umbraco.org/documentation/reference/routing/custom-controllers

    SurfaceControllers are for interactions between clien and server, form handling for example.

    Thanks,

    Alex

  • Greg Miller 9 posts 79 karma points
    Sep 27, 2016 @ 20:36
    Greg Miller
    0

    thanks for the reply Alex.

    So a surface controller and @Html.Action in the master template can get me there but the problem i have is that the object used in the partial view seems to go out of scope once the partial view is rendered.

    Ideally the master template could call a controller which would get an object from a database and return that object as a model, and then that model's properties could be used throughout the template.

    For example, this object's properties are used in the header, in the body and in the footer. I would prefer not to call @html.Action 5 times where each simply returns 1 of the properties with a bit of markup.

  • Alex Skrypnyk 6132 posts 23951 karma points MVP 7x admin c-trib
    Sep 29, 2016 @ 13:50
    Alex Skrypnyk
    100

    Hi Greg,

    I think you have to use Custom Models from, read more here - https://github.com/kgiszewski/LearnUmbraco7/blob/master/Chapter%2006%20-%20Surface,%20WebAPI%20and%20RenderMVC%20Controllers/01%20-%20RenderMVC%20Controllers.md

    Example:

    public class HomePageController : RenderMvcController
        {
            public override ActionResult Index(RenderModel model)
            {
                LogHelper.Info<HomePageController>("Hello, we have a custom model.");
    
                //this line means continue and perform the normal rendering
                return base.Index(new CustomRenderModel());
            }
        }
    
        public class CustomRenderModel : RenderModel
        {
            public string MyNewProperty;
    
            public CustomRenderModel()
                : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent)
            {
                MyNewProperty = "Umbraco rocks!";       
            }
        }
    

    Thanks,

    Alex

  • Greg Miller 9 posts 79 karma points
    Sep 29, 2016 @ 17:56
    Greg Miller
    0

    yup this will work, thanks again Alex

Please Sign in or register to post replies

Write your reply to:

Draft