Umbraco Model Binder - How to get strongly typed objects from child nodes
I'm using Umbraco's Model Binder to generate strongly typed models from my document types to use in my code.
This is working pretty well but I want to know how I can get strongly typed objects for the children of any given generated model.
Here is an example:
public ActionResult Index(HomePage model)
{
var components = model.Children.Where(x => x.DocumentTypeAlias == PageComponentsFolder.ModelTypeAlias)
.Single().Children;
}
HomePage is a strongly typed class generated by the Umbraco model builder. Under the home page node I have a page components folder with several other nodes that all inherit from a ComponentsBaseClass.
How can I make my components variable above a strongly typed list of objects.
Note: If you are using composition, then you can request all pages that are composed with a specific page - those pages will implement an interface generated with the class, for example if you compose your pages with a document named BasePage and you need all children that are composed with BasePage you can have something like:
I'm not using composition to create my document types (I should really look in to that, I have heard good things)
But I tried to do this:
var components = model.Children.Where(x => x.DocumentTypeAlias == PageComponentsFolder.ModelTypeAlias)
.Single().Children<ComponentRoot>();
All my different component document types inherit from ComponentRoot, there are no Interfaces as such (or that I know about) when using the model binder.....
I'm thinking I might have to just do it the old way, running out of time :-)
Umbraco Model Binder - How to get strongly typed objects from child nodes
I'm using Umbraco's Model Binder to generate strongly typed models from my document types to use in my code.
This is working pretty well but I want to know how I can get strongly typed objects for the children of any given generated model.
Here is an example:
HomePage is a strongly typed class generated by the Umbraco model builder. Under the home page node I have a page components folder with several other nodes that all inherit from a ComponentsBaseClass.
How can I make my components variable above a strongly typed list of objects.
Is this possible?
I think you should drop the where and just use
Note: If you are using composition, then you can request all pages that are composed with a specific page - those pages will implement an interface generated with the class, for example if you compose your pages with a document named BasePage and you need all children that are composed with BasePage you can have something like:
I'm not using composition to create my document types (I should really look in to that, I have heard good things)
But I tried to do this:
All my different component document types inherit from ComponentRoot, there are no Interfaces as such (or that I know about) when using the model binder.....
I'm thinking I might have to just do it the old way, running out of time :-)
Ok this is what I ended up with in the end, here is an example of how to use strongly typed models generated by the Umbraco model binder.
Is there a way to get the class name from the ModelTypeAlias?
is working on a reply...