What you are looking for is probobly something like this in you view:
@inherits UmbracoTemplatePage
@{
var children = Model.Content.Children.Where("Visible");
}
@foreach (var child in children)
{
var partialView = "/Views/Partials/" + child.DocumentTypeAlias + ".cshtml";
@Html.Partial(partialView, child)
}
And then you need to create banner.cshtml & newsStory.cshtml & imageGallery.cshtml in the Views/Partials folder.
Then make sure that these partials inherits from UmbracoTemplatePage. Like so:
banner.cshtml:
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
var banner = Model.Content; <!-- Since you pass in your child into the partial view, Model.Content in this case will not be the homePage anymore, but instead it will be the banner item. -->
}
<h1>@(banner.GetPropertyValue<string>("header"))</h1> <!-- Assuming you have a Header property on your banner. Just an example! -->
NOTE: With this setup, if you create a new docType and add a item to your home before adding the corresponding partialview, your view will throw an exception, saying that it cant fint a partial view with the name of that doctype. Ideally you´d probobly want to build a helper that checks the filstructure and checks if the partial view actually exists.
i still get the 'Extension methods cannot be dynamically dispatched' error:
CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named
'RenderPartial' but appears to have an extension method by that name.
Extension methods cannot be dynamically dispatched. Consider casting
the dynamic arguments or calling the extension method without the
extension method syntax.
Loop through child nodes and render partial dynamically
i would like to loop through child nodes and render partials where the name of the partial corresponds to the name of the DocumentTypeAlias.
For instance, under a Homepage node, i would like to add the following nodes:
I have the following partial views:
Ideally i would like to be able to add any number of children of whatever type and say:
The following does not work:
with the error:
Any ideas/solutions?
Hi Fergus.
What you are looking for is probobly something like this in you view:
And then you need to create banner.cshtml & newsStory.cshtml & imageGallery.cshtml in the Views/Partials folder. Then make sure that these partials inherits from UmbracoTemplatePage. Like so:
banner.cshtml:
NOTE: With this setup, if you create a new docType and add a item to your home before adding the corresponding partialview, your view will throw an exception, saying that it cant fint a partial view with the name of that doctype. Ideally you´d probobly want to build a helper that checks the filstructure and checks if the partial view actually exists.
Hope this helps!
Hi Dennis
i still get the 'Extension methods cannot be dynamically dispatched' error:
Fergus
Think i am a little closer:
This seems to work, but i need to check that it works as intended, but for the moment i may be ok.
thanks Dennis
Yes, close! But you need to pass in the child node to the partial view to be able to render it.
Thanks Dennis - i was already there... just.
Awesome! Glad it worked out for you! :)
Have a great weekend!
is working on a reply...