Copied to clipboard

Flag this post as spam?

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


  • Michael Harper 23 posts 134 karma points
    Jan 22, 2017 @ 11:20
    Michael Harper
    1

    Best practice for working with surfacecontroller items

    Hi,

    I'm new to Umbraco development and want to pick up any best practices early on.

    I'm following a tutorial that is building a navigation model.

    We have a layout controller that inherits SurfaceController to access the current page and build the nav.

        private List<NavigationListItem> GetNavigationModelFromDatabase()
        {
            IPublishedContent homePage = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "home").FirstOrDefault();
            var nav = new List<NavigationListItem>();
            nav.Add(new NavigationListItem(new NavigationLink(homePage.Url, homePage.Name)));
            nav.AddRange(Utils.GetChildNavigationList(homePage));
            return nav;
        }
    

    I've put some functionality in a "utils" class but the above method obviously needs Umbraco specific stuff from the SurfaceController. Would you leave this here or move it somewhere else?

    Thank you

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 23, 2017 @ 22:59
    Alex Skrypnyk
    100

    Hi Michael

    I would recommend writing navigation traversing code in partial views, without controllers at all. Because all Umbraco content tree context you already have in your representation level other words in Razor views. Also you can create Umbraco macro and attach view code to macro - then you will be able to control caching of output html from Umbraco.

    I would recommend using a surface controller only if you have some custom code for umbraco navigation data, some database lookups or third-party systems integration.

    At Umakers.dk we are using surface controllers almost only for interaction with forms on the pages.

    Hope it will help you.

    Thanks,

    Alex

  • Michael Harper 23 posts 134 karma points
    Jan 24, 2017 @ 09:39
    Michael Harper
    0

    Hi Alex,

    Thanks for this. I do have nav traversing code in a navigation partial but indeed it uses a model built in the controller.

    Can you point me to an example of caching the partial view using a macro?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 24, 2017 @ 09:44
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 24, 2017 @ 09:43
    Alex Skrypnyk
    2

    Hi Michael,

    Umbraco has great helper CachedPartial, example:

    @Html.CachedPartial("MyPartialName", null, 3600)
    

    3600 - duration of cache

    And this is without macros and stuff just add partial view and cache it in the master.

    What is the main reason to put all data to model and pass it through controller - view?

    I think that we should pass the same data what we have in the view one more time.

    Thanks,

    Alex

  • Michael Harper 23 posts 134 karma points
    Jan 24, 2017 @ 09:55
    Michael Harper
    0

    Hi Alex,

    This looks perfect for my needs! I have an MVC background but new to Umbraco.

    Many thanks :)

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Jan 24, 2017 @ 13:10
    Alex Skrypnyk
    0

    Michael, glad to help.

    Have a nice day!

Please Sign in or register to post replies

Write your reply to:

Draft