Copied to clipboard

Flag this post as spam?

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


  • npack 51 posts 297 karma points
    Oct 05, 2023 @ 20:30
    npack
    0

    How to get External Data into Partial View In Blocklist Element

    After watching Paul Seal's video about creating a block list of different content components https://www.youtube.com/watch?v=lqWoNf27hyc&list=PL90L_HquhD-81xTOCLLJZLl1roU6hXPhp&index=9

    I created a block list that can show several types of elements/components so that my site designers can pick and choose a-la-carte what types of content they want on a given page. Pretty Cool!

    Some of my elements/components need data from an external source. It seems that since these elements can be on any page, route hijacking isn't really the right solution for external data since route hijacking seems to be more for a particular url.

    Is there a better strategy for intercepting these moments and injecting something into my model?

    I'm tempted to do my data access from the component view itself but ideally I would define a property on my element so it becomes part of the model then 1) lock it down or hide it, and 2) populate it.

    Would route hijacking even still work? Perhaps I should create a general controller and then examine the model to see if a block list exists that contains my special element?

  • npack 51 posts 297 karma points
    Mar 21, 2024 @ 15:58
    npack
    100

    At first, I was able to get this working by extending the default RenderController used by Umbraco. That let me catch every umbraco page request and examine the requested content, looping through any block lists and searching for a certain element then populating its extended partial class properties. This felt really gross performance-wise and left all this element-specific code in one huge global intercepting controller.

    A better solution, however, is to use .net ViewComponents. This lets each block list element have its own controller, that can populate data from an outside source. They become almost like mini hijacking controllers.

    Interestingly, Paul Seal has another video about viewcomponents (https://www.youtube.com/watch?v=YcVzaXgPknQ) but I went a step further and invoked them from inside my block list elements.

    In this way, the razor markup for my block list elements only contains one line, which invokes a viewcomponent, passing along its own Model to be the model of the viewcomponent

    @await Component.InvokeAsync(nameof(ContactUsComponent), Model)
    

    Then the viewcomponent 'controller' can populate or overwrite any properties on the model (I use a partial class to 'extend' the element view model) The constructor of the view component can be injected with any dependencies you need.

    Its 'Invoke' method can take args, which I used to pass along the model

    public IViewComponentResult Invoke(ContactUsElement contactUsElement) (
        contactUsElement.Hours = "GetMeFromAnApiCall";
        return View(this.GetType().Name, contactUsElement);
    

    and the view component's view contains what would normally be found in the element's cshtml.

Please Sign in or register to post replies

Write your reply to:

Draft