Copied to clipboard

Flag this post as spam?

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


  • Andy Cox 31 posts 182 karma points
    Dec 15, 2017 @ 12:30
    Andy Cox
    0

    Is it possible to apply multiple Models to a View Model?

    Hi All,

    Is there a way to apply multiple Models to a View Model in Ditto?

    I have a HomePageModel, which inherits from a BasePageModel. The HomePageModel has page specific properties but I would also like to apply some global site settings to the BasePageModel (such as google analytics api key etc...) as well.

    For example, I can map to my page model:

    var pageModel = model.Content.As<HomePageModel>();
    

    But then I would like to apply my global settings to the model by passing the original pageModel instance:

    pageModel = globalSettingsContent.As(CurrentCulture, pageModel);
    

    This unfortunately seems to wipe out or overwrite some of original properties (the Id, Url etc..).

    Alternatively is there a Ditto attribute I can apply to properties to ignore them if they've already been mapped?

    Sorry if this isn't very clear. I can provide futher code examples, if needed, to try and detail what we are trying to achieve.

    Thanks for any help.

    Andy

  • Andy Cox 31 posts 182 karma points
    Dec 15, 2017 @ 16:21
    Andy Cox
    100

    I solved my issue by just converting each of my models separately and manually adding them to the pageModel instance.

    E.g.

    var pageModel = model.Content.As<HomePageModel>();
    
    pageModel.Header = globalSettings.As<HeaderViewModel>();
    pageModel.Footer = globalSettings.As<FooterViewModel>();
    pageModel.SiteSettings = globalSettings.As<SiteSettingsViewModel>();
    pageModel.CookieNoticeViewModel = globalSettings.As<CookieNoticeViewModel>();
    etc...
    

    It would have been nice to able to do this in one shot though!

    Thanks,

    Andy

  • Lee Kelleher 3945 posts 15163 karma points MVP 10x admin c-trib
    Dec 18, 2017 @ 11:38
    Lee Kelleher
    1

    Hi Andy,

    It is possible to do it all-in-one, but it would either need a custom processor (so Ditto knows where to get the globalSettings node from), or you could use the UmbracoXPath processor attribute - again, it'd need the XPath relative to the current page/node.

    For the XPath one, an example would be like this...

    public class HomePageModel
    {
        [UmbracoXPath("$site/globalSettings[@isDoc]")]
        public HeaderViewModel Header { get; set; }
    }
    

    Of course, I have no idea what the XPath is to your globalSettings node is

    Hope that helps?

    Cheers,
    - Lee

  • Andy Cox 31 posts 182 karma points
    Dec 18, 2017 @ 12:16
    Andy Cox
    0

    That definely a method to keep in mind.

    Thanks Lee.

  • 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