Copied to clipboard

Flag this post as spam?

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


  • Simon Bowler 14 posts 34 karma points
    Sep 10, 2012 @ 15:48
    Simon Bowler
    0

    Razor script, pulling image from child page

    Hi,

    I have a page on my site with a list of sub pages, I want to link to the sub pages by puilling out an image and wrapping it in an achor.

    The trouble with the code below is it displays

    <a href="link01">               
                <img src="image01">
                <img src="image02">
    </a>
    <a href="link02">               
                <img src="/image01">
                <img src="image02">
    </a>

    Instead of

    <a href="link01">               
                <img src="image01">
    </a>
    <a href="link02">               
                <img src="image02">
    </a>

    Here is my code

             @foreach (var page in @Model.Children) {  
               <a class="fancybox" href="@page.Url">               
               @foreach(var pressimg in Model.Children)
               {
                <img src="@pressimg.Media("pressImage", "umbracoFile")" alt="@pressimg.Name"/>
                }
               </a>
              
  • Douglas Ludlow 210 posts 366 karma points
    Sep 10, 2012 @ 16:13
    Douglas Ludlow
    0

    Well you've got nested foreach loops that are both looping through the current pages children. If I understand what you're trying to do, you can probably do this all in a single foreach loop:

    @foreach (var page in Model.Children)
    {
    <a class="fancybox" href="@page.Url">
    <img src="@page.Media("pressImage", "umbracoFile")" alt="@page.Name"/>
    </a>
    }

     

  • Simon Bowler 14 posts 34 karma points
    Sep 10, 2012 @ 16:47
    Simon Bowler
    0

    Thats done it.. thanks!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies