Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Fergus Davidson 309 posts 588 karma points
    Nov 03, 2016 @ 16:29
    Fergus Davidson
    1

    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:

    • banner [banner]
    • news [newsStory]
    • image gallery [imageGallery]

    I have the following partial views:

    • banner
    • newsStory
    • imageGallery

    Ideally i would like to be able to add any number of children of whatever type and say:

    render all of the children using their appropriate partial views

    The following does not work:

    @{ var selection = CurrentPage.Children.Where("Visible");}
    
    @foreach(var item in selection){
        var dd = item.DocumentTypeAlias;
        Html.RenderPartial(dd); 
    
    }
    

    with the error:

    CS1973: 'System.Web.Mvc.HtmlHelper

    Any ideas/solutions?

  • Dennis Adolfi 1082 posts 6445 karma points MVP 5x c-trib
    Nov 04, 2016 @ 07:45
    Dennis Adolfi
    2

    Hi Fergus.

    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.

    Hope this helps!

  • Fergus Davidson 309 posts 588 karma points
    Nov 04, 2016 @ 09:34
    Fergus Davidson
    0

    Hi Dennis

    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.

    Fergus

  • Fergus Davidson 309 posts 588 karma points
    Nov 04, 2016 @ 09:39
    Fergus Davidson
    0

    Think i am a little closer:

    @{var children = CurrentPage.Children.Where("Visible");}    
    @foreach (var child in children)
        {
            var partialView = "/Views/Partials/homepage/" + child.DocumentTypeAlias + ".cshtml";
            @Html.Partial((string)partialView)
        }
    

    This seems to work, but i need to check that it works as intended, but for the moment i may be ok.

    thanks Dennis

  • Dennis Adolfi 1082 posts 6445 karma points MVP 5x c-trib
    Nov 04, 2016 @ 09:59
    Dennis Adolfi
    100

    Yes, close! But you need to pass in the child node to the partial view to be able to render it.

    @{var children = Model.Content.Children.Where("Visible");}    
    @foreach (var child in children)
        {
            var partialView = "/Views/Partials/homepage/" + child.DocumentTypeAlias + ".cshtml";
            @Html.Partial((string)partialView, (IPublishedContent)child)
        }
    
  • Fergus Davidson 309 posts 588 karma points
    Nov 04, 2016 @ 10:01
    Fergus Davidson
    1

    Thanks Dennis - i was already there... just.

  • Dennis Adolfi 1082 posts 6445 karma points MVP 5x c-trib
    Nov 04, 2016 @ 10:05
    Dennis Adolfi
    0

    Awesome! Glad it worked out for you! :)

    Have a great weekend!

Please Sign in or register to post replies

Write your reply to:

Draft