Copied to clipboard

Flag this post as spam?

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


  • Roy Berris 89 posts 578 karma points c-trib
    Jun 03, 2022 @ 11:17
    Roy Berris
    0

    Umbraco 9: Get ContentModel in a ViewComponent

    Hi, how would one get the ContentModel (or IPublishedContent) in a ViewComponent?

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Jun 03, 2022 @ 15:28
    Alex Skrypnyk
    101

    Hi Roy,

    This is an example of getting current page published content model in ViewComponent class:

    public class MetaDataViewComponent : ViewComponent
        {
            private IUmbracoContextAccessor _context;
    
            public MetaDataViewComponent(IUmbracoContextAccessor context)
            {
                _context = context;
            }
            public async Task<IViewComponentResult> InvokeAsync()
            {
                var content = _context.GetRequiredUmbracoContext().PublishedRequest.PublishedContent;
    
                return View(model);
            }
        }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies