Copied to clipboard

Flag this post as spam?

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


  • Imran 17 posts 108 karma points c-trib
    Feb 16, 2022 @ 15:42
    Imran
    0

    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?

  • Mark Drake 121 posts 410 karma points c-trib
    Feb 16, 2022 @ 17:31
    Mark Drake
    1

    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.

  • Imran 17 posts 108 karma points c-trib
    Feb 16, 2022 @ 18:09
    Imran
    101

    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)

    enter image description here

  • Gerty Engrie 129 posts 482 karma points c-trib
    Jul 05, 2022 @ 15:55
    Gerty Engrie
    1

    Just adding the "copyable" version ;-)

     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);
            }
     }
    
Please Sign in or register to post replies

Write your reply to:

Draft