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:
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
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:
But then I would like to apply my global settings to the model by passing the original pageModel instance:
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
I solved my issue by just converting each of my models separately and manually adding them to the pageModel instance.
E.g.
It would have been nice to able to do this in one shot though!
Thanks,
Andy
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 theUmbracoXPath
processor attribute - again, it'd need the XPath relative to the current page/node.For the XPath one, an example would be like this...
Of course, I have no idea what the XPath is to your
globalSettings
node isHope that helps?
Cheers,
- Lee
Hello Andy ,
Thanks for example but i am new to umbraco cms i want to know what's globalSettings here
Thanks in advance.
That definely a method to keep in mind.
Thanks Lee.
is working on a reply...