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 4020 posts 15802 karma points MVP 13x 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

  • Mashiyat Husain 9 posts 79 karma points
    May 02, 2023 @ 06:47
    Mashiyat Husain
    0

    Hello Andy ,

    Thanks for example but i am new to umbraco cms i want to know what's globalSettings here

    Thanks in advance.

  • 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.

Please Sign in or register to post replies

Write your reply to:

Draft