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 38 posts 375 karma points
    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 (skrypnyk.dev) 6061 posts 23313 karma points MVP 6x admin c-trib
    Jun 03, 2022 @ 15:28
    Alex Skrypnyk (skrypnyk.dev)
    100

    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