Copied to clipboard

Flag this post as spam?

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


  • BTrigger 4 posts 94 karma points
    Jul 26, 2022 @ 17:35
    BTrigger
    0

    Unable to use properties of List<IPublishedContent> on view

    Hi,

    I have a content list wich im getting like this:

    var selectedTestimonials = Umbraco.ContentAtRoot().Where(x => x.IsDocumentType("testimonialList")).FirstOrDefault().Children().OrderBy(t => Guid.NewGuid()).Take(10).ToList();

    But then i can't use any item properties in the view unless i use a partial that takes the item as the model and inherits from:

    @inherits Umbraco.Cms.Web.Common.Views.UmbracoViewPage<Testimonial>
    

    Any idea of what im doing wrong? Thanks in advance.

  • Marc Goodson 2157 posts 14435 karma points MVP 10x c-trib
    Jul 27, 2022 @ 08:43
    Marc Goodson
    101

    Hi BTrigger

    IPublishedContent is an interface that describes the key properties of a generic published content item in Umbraco, so you will see there are CreateDate, Id etc properties on your selectedTestimonials.

    But that list isn't aware it's a list of Testimonial objects that implement IPublishedContent, which is why you can't see the properties you expect to be available.

    You can access properties on an IPublishedContent using .Value<T>("propertyAlias") - but really you'd probably rather they were all Testimonial objects....

    which you can achieve by using the .OfType<T>() extension method...

    eg

    var selectedTestimonials = Umbraco.ContentAtRoot().Where(x => x.IsDocumentType("testimonialList")).FirstOrDefault().Children().OfType<Testimonial>().OrderBy(t => Guid.NewGuid()).Take(10).ToList();
    

    should result in a list of Testimonial objects and give you direct access to the properties.

    regards

    Marc

  • BTrigger 4 posts 94 karma points
    Jul 27, 2022 @ 09:01
    BTrigger
    0

    Hi Marc,

    Works like a charm! Thanks for the great explanation and solution!

    Cheers

  • 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