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

Write your reply to:

Draft