Previously when we used child actions we would have access to the "CurrentPage" IPublishedContent, but I can't seem to find this on View Componenets in V9
I call my View Components from my Master template and could just pass the Model as a parameter, but some of my pages have hijacked controllers which convert the IPublishedController to a ViewModel, so it'll be missing all the IPublishedContent I need from the CurrentPage model
Is there an easy way of accessing the "CurrentPage" IPublishedContent on V9 View Components, just like we did in child actions on V8?
It seems like the best solution for now is injecting the "IUmbracoContextAccessor" demonstrated in the above post
This does not require the Model to be passed into the ViewComponent as it will always look for the current Umbraco IPublishedContent page (even if you are hijacking the page and passing in your own View Model)
public class TestViewComponent : ViewComponent
{
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
public TestViewComponent(IUmbracoContextAccessor umbracoContextAccessor)
{
_umbracoContextAccessor = umbracoContextAccessor;
}
public IViewComponentResult Invoke()
{
var currentpage = _umbracoContextAccessor?.GetRequiredUmbracoContext()?.PublishedRequest?.PublishedContent;
var model = .... ;
return View("Index", model);
}
public async Task<IViewComponentResult> InvokeAsync()
{
var currentpage = _umbracoContextAccessor?.GetRequiredUmbracoContext()?.PublishedRequest?.PublishedContent;
var model = .... ;
return View("Index", model);
}
}
ViewComponent CurrentPage Model
Previously when we used child actions we would have access to the "CurrentPage" IPublishedContent, but I can't seem to find this on View Componenets in V9
I call my View Components from my Master template and could just pass the Model as a parameter, but some of my pages have hijacked controllers which convert the IPublishedController to a ViewModel, so it'll be missing all the IPublishedContent I need from the CurrentPage model
Is there an easy way of accessing the "CurrentPage" IPublishedContent on V9 View Components, just like we did in child actions on V8?
I don't have a lot of experience with View Components, but could you access
Umbraco.AssignedContentItem
from it?Here's a ref to the source code.
Hi Mark,
Thanks for the suggestion
Looks like there may be some problems trying to access the "Umbraco.AssignedContentItem" with the following error:
"InvalidOperationException: Cannot return the IPublishedContent because the UmbracoHelper was not constructed with an IPublishedContent"
It is mentioned in this post:
https://our.umbraco.com/forum/umbraco-9/106681-umbraco-context-in-viewcomponent-best-practice
It seems like the best solution for now is injecting the "IUmbracoContextAccessor" demonstrated in the above post
This does not require the Model to be passed into the ViewComponent as it will always look for the current Umbraco IPublishedContent page (even if you are hijacking the page and passing in your own View Model)
Just adding the "copyable" version ;-)
is working on a reply...