Copied to clipboard

Flag this post as spam?

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


  • Mohammad Javed 64 posts 373 karma points
    Mar 13, 2015 @ 16:40
    Mohammad Javed
    0

    List Child Pages from a certain Node Id (Razor - Macro Partials)

    Hi,
    I am having trouble rendering a macro in my MVC View - Below is the code to list child pages from a certain node number. I get the following error message;
    The view at '~/Views/MacroPartials/BannersLister.cshtml' must derive from WebViewPage, or WebViewPage.
    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @*
        Model = The current page the macro is executed on
                @Model.bodyText
    
        Parameter = collection of parameter values passed from the macro
                @Paramter.myParam
    
        Library = utillity library with common methods
                @Library.NodeById(1233)
    *@
    
    @* The fun starts here *@
    
    @if (Model.NodeById(1073).Children.Any())
    {
        foreach (var banner in Model.NodeById(1073).Children)
        {            
            
    @banner.Name
    } }

    Under Macro properties the file to render is MVC Partial View. http://prntscr.com/6g9f4j

    My nodes; http://prntscr.com/6g9fkz

    Can't quite get my head around the error message. Any help/guidance would be appreciated.

    Thank you

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Mar 15, 2015 @ 08:41
    Jan Skovgaard
    0

    Hi Mohammad

    To me it looks like you're using some old Razor syntax - Is it something you have found elsewhere and copy/pasted? If so you can't rely on it to work since the razor implementation has changed many times since Razor was introduced in Umbraco 4.6 and it seems like you're using v7 :)

    If you create a new partial view macro in the developer section in the backoffice you'll see that it by default is using this statement in the top @inherits Umbraco.Web.Macros.PartialViewMacroPage

    Apart from wanting to render som child nodes it seems like actually want to render som images for a banner? So before throwing a possible solution after you it would be nice to know exactly what it is you're trying to achieve?

    Perhaps you can get some ideas on how to achieve it by trying to have a play with some of the pre-defined scripts that it's possible to choose from in the "create" dialogue from the dropdown.

    Looking forward to hearing from you so we can get this one solved :)

    /Jan

  • Mohammad Javed 64 posts 373 karma points
    Mar 16, 2015 @ 10:38
    Mohammad Javed
    100

    Right - I've just written my own little script, come across the following script and tweaked it to my requirements, and it makes me understand it alot better

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    
    @* The fun starts here *@
    <div class="bannersContainer">
    @foreach (var childBanner in Model.Content.Children.Where(c => c.DocumentTypeAlias.Equals("BannersIndex")))
    {
        <div>
            @if (childBanner.Children.Any(x => x.DocumentTypeAlias.Equals("Banner")))
            {
                <div>
                    @foreach (var page in childBanner.Children.Where(p => p.DocumentTypeAlias.Equals("Banner")))
                    {
                        <div class="pageName">
                            <a href="@page.Url">@page.Name</a>
                        </div>
                    }
                </div>
            }
        </div>
    }
    

    Jobs a good'un! You've just got to love Umbraco! <3

  • Mohammad Javed 64 posts 373 karma points
    Mar 16, 2015 @ 13:28
    Mohammad Javed
    0

    Right, i've tried the pre-defined script that lists children of a certain doc type. Code below

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @*
    This snippet shows how simple it is to fetch only children of a certain Document Type using Razor. Instead of
    calling .Children, simply call .AliasOfDocumentType in plural.
    For instance .Textpages or .NewsArticles (you can find the alias of your Document Type by editing it in the 
    Settings section).
    *@
    
    @{    
    @*Build a query and return the visible items *@
    var selection = CurrentPage.BannerIndex.Where("Visible");
    
    @*
       Example of more querying, if you have a true/false property with the alias of shouldBeFeatured:
       var selection= Model.Textpages.Where("shouldBeFeatured == true").Where("Visible");
    *@
    }
    
    @*Determine if there are any nodes in the selection, then render list *@
    @if(selection.Any()){
    
    foreach (var childBanner in selection)
    {                
       <div class="name">@childBanner.Name</div>
    }             
    
    }
    

    It shows the banners root node, however i cannot get it to display the doctype banner (child node) to display.

Please Sign in or register to post replies

Write your reply to:

Draft