Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Aug 09, 2012 @ 20:29
    Craig100
    0

    Razor use of First()

    Iterating through a foreach loop, can we detect the first iteration by using First(), without setting a counter as in:-

     

    @{
      //Check there are slider image page loaded
      var theCount = @Model.Descendants("SliderImagePage").Count();
      
      if (theCount > 0)
        {
            foreach (var theImagePage in Model.Descendants("SliderImagePage"))
              {
               var theImage = theImagePage.Media("sliderImage","umbracoFile");
               if (theImagePage.First() == true) {
                @:<div class="slide" style="background-image:url('@Html.Raw(theImage)');display:block;"></div>
                } else {
                @:<div class="slide" style="background-image:url('@Html.Raw(theImage)');display:none;"></div>
                }
              }    
        
         }
      else
      {
       @: No Picture Image pages set up
      }
      
    }

    The above doesn't work but I can't work out how it should.

    The site is Umbraco V4.7.2 

    Any pointers would be helpful.

    Thanks

     

    Craig

     

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 09, 2012 @ 20:46
    Fuji Kusaka
    0

    Hi Craig,

    How weird i just posted a solution of mine a few minutes before where i was looking for the Last() item in media folder.

    My solution as 

    if(img.IsLast()){
    //do something
    }
    Have you try if(theImagePage.IsFirst()){}

    Did you have a look at the Razor Dynamic Cheat Sheet

     

  • Craig100 1136 posts 2523 karma points c-trib
    Aug 09, 2012 @ 21:56
    Craig100
    0

    Thanks, that worked:)

    I have the cheat sheet printed out here right now, but didn't see the IsHelpers section! This is the problem with dipping in and out of various technologies during the day :(

    Thanks 

    Cheers,

    Craig

  • Fuji Kusaka 2203 posts 4220 karma points
    Aug 09, 2012 @ 22:15
    Fuji Kusaka
    0

    Hi Craig,

    Glad you got this working.

    //fuji

Please Sign in or register to post replies

Write your reply to:

Draft