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?
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.
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.
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
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
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?
Read more about chachedPartial:
https://our.umbraco.org/documentation/reference/templating/mvc/partial-views#caching
Hi Michael,
Umbraco has great helper CachedPartial, example:
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
Hi Alex,
This looks perfect for my needs! I have an MVC background but new to Umbraco.
Many thanks :)
Michael, glad to help.
Have a nice day!
is working on a reply...