Copied to clipboard

Flag this post as spam?

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


  • andrew shearer 506 posts 653 karma points
    Sep 07, 2021 @ 23:24
    andrew shearer
    0

    inject AssignedContentItem into partial view?

    Hi - I have a partial view to render an IPublishedElement model, but also need to show content from the current page (IPublishedContent). Can i obtain the current IPC from the DI container?

    this is what i tried but it doesn't work:

    @inject UmbracoHelper  UmbracoHelper
    @{
        var ipc = UmbracoHelper.AssignedContentItem;
    }
    

    thanks

  • andrew shearer 506 posts 653 karma points
    Sep 09, 2021 @ 02:06
    andrew shearer
    1

    so i managed to find this technique which seems to give me what im after:

    @inject IUmbracoContextAccessor umbracoContext
    
    @{
        var currentPage = umbracoContext.GetRequiredUmbracoContext().PublishedRequest.PublishedContent;
    }
    

    But still keen to hear thoughts on if this is an antipattern/bad practice and why, or if there is a better way.

    Thanks :D

Please Sign in or register to post replies

Write your reply to:

Draft