Copied to clipboard

Flag this post as spam?

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


  • David Hyslop 27 posts 181 karma points
    May 22, 2023 @ 09:34
    David Hyslop
    0

    Getting custom data types from Model.Children

    Hi

    I'm currently migrating some sites to Umbraco cloud (V7 - v11).

    I'm building a landing page that just displays child pages.

    Using this code

    <ul>
    @foreach(var item in Model.Children)
    {
        <li><a href="@item.Url()">@item.Name</a></li>
    }
    

    Within the child pages is a data type called metaDescription, I'd like to display the value from metaDescription in the foreach.

           <ul>
        @foreach(var item in Model.Children)
        {
            <li><a href="@item.Url()">@item.Name</a>
    @item.metaDescription 
    
    </li>
        }
    </ul>
    

    I get the error 'IPublishedContent' does not contain a definition for 'metaDescription' and no accessible extension method 'metaDescription' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    Any ideas how I can do this?

    Thanks in advance

  • Sotiris Filippidis 286 posts 1501 karma points
    May 22, 2023 @ 09:49
    Sotiris Filippidis
    101

    The metaDescription property is specific to a doctype, and you're getting IPublishedContent in your loop. So "item" is just an IPublishedContent which cannot know the specific properties you're using.

    You either have to go with item.Value("metaDescription") (which is not very desirable due to magic strings) or go with Model.Children<T> where T would be the child nodes' doctype alias, so that "item" is mapped to that specific class in Models Builder that actually contains the metaDescription property.

  • David Hyslop 27 posts 181 karma points
    May 22, 2023 @ 09:58
    David Hyslop
    0

    Perfect thanks @item.Value("metaDescription") has worked.

Please Sign in or register to post replies

Write your reply to:

Draft