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 576 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 6131 posts 23950 karma points MVP 7x 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