Copied to clipboard

Flag this post as spam?

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


  • Chris Turnbull 7 posts 27 karma points
    Aug 05, 2014 @ 14:05
    Chris Turnbull
    0

    Creating a loop of child nodes and fields from fixed parent node

    Hi Guys, 

    I'm new to razor and am struggling with this partial based macro. Trying to include an image in which is selected on the child node via a media picker with the attribute ID of sliderImage. I can get the image node number but cant convert it to the its url. 

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @if (Model.MacroParameters["startNodeID"] != null)

    {

        @* Get the start node as a dynamic node *@

        var startNode = Umbraco.Content(Model.MacroParameters["startNodeID"]);

        if (startNode.Children.Where("Visible").Any())

        {

          foreach (var page in startNode.Children.Where("Visible"))

          {

    @:<section id="@page.Name" class="homepage-section wait-for-load">

    @:<div class="title-wrap" style="background-image:@page.sliderImage">

    @:<div class="title-content">

            @:<h2><a href="@page.Url"></a>@page.Name</h2>

            @:<a href="@page.Url" class="more-link">@page.Name</a>

            @:</div>

            @:</div>

    @:</section>

          }

        }    

    }

    Any pointers would be greatly appreciated! 
    Thanks
  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 05, 2014 @ 15:14
    Dan Lister
    0

    Hi Chris,

    You'll want to convert the media ID to a TypedMedia instance as follows:

    @{
        var startNode = Umbraco.TypedContent(Model.MacroParameters["startNodeID"]);
    
        foreach (var page in startNode.Children.Where("Visible"))
        {
            var image = Umbraco.TypedMedia(page.GetPropertyValue<int>("sliderImage"));
            var imageUrl = image.Url;
        }
    }
    

    Thanks, Dan.

  • Chris Turnbull 7 posts 27 karma points
    Aug 06, 2014 @ 03:26
    Chris Turnbull
    0

    Hi Dan, 

    I actually came across a few example like that, have tried but am getting the following:

    Error loading Partial View script (file: ~/Views/MacroPartials/splash-pages.cshtml)

    I must be missing something very simple somewhere.... 

    Chris

     

  • Chris Turnbull 7 posts 27 karma points
    Aug 06, 2014 @ 04:14
    Chris Turnbull
    0

    The actual error logs state:

     

    Error loading partial view macro (View: ~/Views/MacroPartials/splash-pages.cshtml). Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

       at CallSite.Target(Closure , CallSite , Object )

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 06, 2014 @ 09:16
    Dan Lister
    0

    Hmmm... I'd check the macro property 'startNodeID' exists and has the correct alias. I'd also check the 'sliderImage' property exists and has the correct alias as well. Do you know which line the error is appearing on?

    Thanks, Dan.

  • Chris Turnbull 7 posts 27 karma points
    Aug 06, 2014 @ 11:48
    Chris Turnbull
    0

    Not sure... I had to get the site delivered. Resorted to xlst based macro. I need to get my head around razor more. 

  • Chris Turnbull 7 posts 27 karma points
    Aug 06, 2014 @ 11:50
    Chris Turnbull
    0

    It seem all correct.. Was running the latest version, did i need ucomponents installed? Not sure if its supported on 7.x.x

  • Dan Lister 416 posts 1974 karma points c-trib
    Aug 06, 2014 @ 11:54
    Dan Lister
    0

    uComponents only works with v6 and below. nuPickers is a good v7 alternative to some of the data types present in uComponents.

    Thanks, Dan.

Please Sign in or register to post replies

Write your reply to:

Draft