I wanted to return a list of my nested document types objects, but dont know how.
So, at first, I tried to just use my Nested property, but it return a IEnumerable<IPublishedContent> instead list of my strongly typed nested objects. It does't let me use direct syntax like item.MyProperty.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<HomePage>
@{
// does'not works
var items = Model.Content.MyNestedItems;
foreach (var item in items)
{
@item.MyProperty // Not found coz it a IPublishedContent
}
}
@{
var ncItems = Model.Content.MyNestedItems;
foreach (var ncItem in ncItems)
{
// Article - is MY model
// You might want to do a switch/if statement to assign the nc Item to the right Model
// A way to determine the type of ncItem is by it's DTA
// string nodeAlias = ncItem.DocumentTypeAlias;
var item = new Article(ncItem);
@item.MyProperty
}
}
@{
var items = Model.Content.ContentModules;
foreach (var ncItem in items)
{
switch (ncItem.DocumentTypeAlias)
{
case "CM_basicTextModule":
var basicTextModule = new Cm_basicTextModule(ncItem);
@Html.Partial("CM_basicTextModule", basicTextModule);
break;
case "CM_halfBoxes":
var halfBoxes = new Cm_halfBoxes(ncItem);
@Html.Partial("CM_halfBoxes", halfBoxes);
break;
case "CM_fullWidthBordered":
var fullWidthBordered = new Cm_fullWidthBordered(ncItem);
@Html.Partial("CM_fullWidthBordered", fullWidthBordered);
break;
}
}
so I ended up doing something like this. Then I used a normal view to render out each one the way I needed. I'm sure I can tidy it up as I go.
@foreach (var module in Model.NcModules.OrderBy(x => x.SortOrder))
{
switch (module.DocumentTypeAlias)
{
case "moduleTitle":
@Html.Partial("Modules/Title", new ModuleTitle(module))
break;
case "moduleText":
@Html.Partial("Modules/Text", new ModuleText(module))
break;
case "moduleTextAndImage":
@Html.Partial("Modules/TextAndImage", new ModuleTextAndImage(module))
break;
case "moduleImage":
@Html.Partial("Modules/Image", new ModuleImage(module))
break;
case "moduleImageGallery":
@Html.Partial("Modules/ImageGallery", new ModuleImageGallery(module))
break;
case "moduleReviewPicker":
@Html.Partial("Modules/ReviewPicker", new ModuleReviewPicker(module))
break;
case "moduleQuotePicker":
@Html.Partial("Modules/QuotePicker", new ModuleQuotePicker(module))
break;
case "moduleVideo":
@Html.Partial("Modules/Video", new ModuleVideo(module))
break;
case "moduleShare":
@Html.Partial("Modules/Share")
break;
case "moduleProjectPicker":
@Html.Partial("Modules/ProjectPicker", new ModuleProjectPicker(module))
break;
case "moduleButton":
@Html.Partial("Modules/Button", new ModuleButton(module))
break;
case "modulePagePicker":
@Html.Partial("Modules/PagePicker", new ModulePagePicker(module))
break;
case "moduleSubPages":
@Html.Partial("Modules/SubPages")
break;
case "moduleSiblings":
@Html.Partial("Modules/Siblings")
break;
default:
break;
}
}
@foreach (var cp in Model.NestedContent)
{
switch (cp.DocumentTypeAlias)
{
case FeaturedTopicPanel.ModelTypeAlias:
@Html.Partial(cp.DocumentTypeAlias, new FeaturedTopicPanel(cp));
break;
case MultipleArticlePanel.ModelTypeAlias:
@Html.Partial(cp.DocumentTypeAlias, new MultipleArticlePanel(cp));
break;
case PodPanel.ModelTypeAlias:
@Html.Partial(cp.DocumentTypeAlias, new PodPanel(cp));
break;
case SingleVideoPanel.ModelTypeAlias:
@Html.Partial(cp.DocumentTypeAlias, new SingleVideoPanel(cp));
break;
}
}
How to return strongly typed object from Nested Content?
Using the Nested Content.
I wanted to return a list of my nested document types objects, but dont know how.
So, at first, I tried to just use my Nested property, but it return a
IEnumerable<IPublishedContent>
instead list of my strongly typed nested objects. It does't let me use direct syntax likeitem.MyProperty
.How to do it propely?
Hi Dmitriy,
Can you try the following:
OR you could try:
Let me know how it goes.
Regards
Craig
Thanks, Craig. It helped, but it still doesn't let use a direct syntax.
Do an other options exist?
May be a library or other approach?
Thank you, Paul. It is exactly what i wanted!
Now, I have an idea to create a generic class to use it as a model instead UmbracoTemplatePage
I usually do like
I think you could also do
But as Paul wrote, a switch for determining the model if there is more than one type in the nest, would be necessary.
Tnak you, Søren I will use it too.
so I ended up doing something like this. Then I used a normal view to render out each one the way I needed. I'm sure I can tidy it up as I go.
Looks good to me
You could probably refactor it a bit more, as the var declaration is a bit superfluous
Yeah, you're right. Still a work in progress :)
UPDATE: Now I using LINQ extension method
.Cast<Type>()
I dont know is it a good idea on performance, but it works and code looks good.
What do you think?
Depends if you can guarantee that all your NestedContent nodes are of a single Model type, and not null.
As with a Cadburys creme egg - there's plenty of different ways/methods to eat it :-)
Good remark, Paul! Thanks.
But I did't encounter with cases when need more then one type in nested content.
Could you give an example?
Here is a little snippet that might help Dimitri
is working on a reply...