Copied to clipboard

Flag this post as spam?

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


  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Nov 13, 2015 @ 15:53
    Ismail Mayat
    0

    PublishedContentModelFactory with parent

    Guys,

    I have got the PublishedContentModelFactory working nicely. So I have DestinationItem and that has property TravelTips. I also have Destinations item which is parent of DestinationItem and that also has TravelTips property.

    What I am doing is

    if (currentDestination.TravelTips != null && currentDestination.TravelTips.Any())
                {
                    @Html.Partial("~/Views/Partials/Shared/_TravelTips.cshtml", currentDestination.TravelTips)
                }
                else
                {
                    var parent = (Destinations)currentDestination.Parent;
                    if (parent.TravelTips != null && parent.TravelTips.Any())
                    {
                        @Html.Partial("~/Views/Partials/Shared/_TravelTips.cshtml", parent.TravelTips)
                    }
                }
    

    In my else statement why do i have todo the cast if i dont then i cannot get TravelTips on parent. Am I missing something?

    Regards

    Ismail

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Nov 13, 2015 @ 16:44
    Lee Kelleher
    100

    Hey Ismail,

    At that point it's nothing really to do with Ditto, it's a ModelFactory thing.

    With the currentDestination.Parent the runtime/compiler thinks you're only using IPublishedContent, it has no idea of the intended type.

    You can use the Parent<T> extension method, (from the "Umbraco.Web" namespace) to cast it to the intended type (of derived IPublishedContent).

    var parent = currentDestination.Parent<Destinations>();
    

    Hope that helps?

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft