Copied to clipboard

Flag this post as spam?

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


  • RunnicFusion 62 posts 145 karma points
    Sep 11, 2012 @ 11:51
    RunnicFusion
    0

    Razor count items

    How can i do this in razor:

    • when there is one item, i only want this item to be displayed. (item in fotoGallerij)
    • when there are more items, i want all of them (like the code bellow, working)

    How can i make this if (i think) structure in razor (c# / umbraco)?

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    <ul class="image-gallery">
    @foreach (var item in @Model.fotoGallerij)
    {
    <li>
    <a class="gallery grouped" href="/ImageGen.ashx?height=500&amp;constrain=true&amp;crop=resize&amp;[email protected]" title="">

    <img src="/ImageGen.ashx?width=71&amp;height=73&amp;crop=resize&amp;[email protected]" alt=""/></a>
     
    </li>
     }
    </
    ul>
     
    <script>
        $
    ("a.gallery").colorbox({rel:'grouped'});
    </script>

    Thanks for helping!
  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 11, 2012 @ 12:01
    Jeroen Breuer
    0

    Hello,

    Are @Model.fotoGallerij in you foreach loop children of your current node? Because there is a count method you can use: http://umbraco.com/follow-us/blog-archive/2011/2/23/umbraco-47-razor-feature-walkthrough-%E2%80%93-part-1.aspx.

    But if you are looping through the children won't this just always work? If there is only one child node it will only loop once and simply only show one image.

    Jeroen

  • RunnicFusion 62 posts 145 karma points
    Sep 11, 2012 @ 12:03
    RunnicFusion
    0

    @Jeroen Breuer: Yes, he only shows one image, but that one needs to be bigger (full size in gallery) and has a link bellow it. (now it opens colorbox).

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 11, 2012 @ 12:11
    Jeroen Breuer
    0

     

    Hmm perhaps somethings like this?

    @inherits umbraco.MacroEngines.DynamicNodeContext
    
    @if(Model.fotoGallerij.Count() == 1)
    {
    <img src="@Model.fotoGallerij.Image.umbracoFile" alt=""/>
    }
    else
    {
    <ul class="image-gallery"> foreach (var item in @Model.fotoGallerij) { <li> <a class="gallery grouped" href="/ImageGen.ashx?height=500&amp;constrain=true&amp;crop=resize&amp;[email protected]" title=""> <img src="/ImageGen.ashx?width=71&amp;height=73&amp;crop=resize&amp;[email protected]" alt=""/></a> </li> } </ul> <script> $("a.gallery").colorbox({rel:'grouped'}); </script>

    Jeroen

     

  • RunnicFusion 62 posts 145 karma points
    Sep 11, 2012 @ 12:16
    RunnicFusion
    0

    @Jeroen Breuer: Thanks, that looks like a good solution to me. but:

    i'll get: 'umbraco.item' does not contain a definition for 'Image'

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 11, 2012 @ 16:19
    Jeroen Breuer
    0

    Could you post the entire stack trace when you get the error? You can do this by adding ?umbDebugShowTrace=true to the querystring.

    Jeroen

  • RunnicFusion 62 posts 145 karma points
    Sep 11, 2012 @ 17:02
    RunnicFusion
    0

    How to do that? i've googled on that but i don't get more error information

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 11, 2012 @ 17:05
    Jeroen Breuer
    0

    If it's not working that means it's probably disabled. Try setting this in the web.config:

    <add key="umbracoDebugMode" value="true"/>

    Jeroen

  • RunnicFusion 62 posts 145 karma points
    Sep 11, 2012 @ 17:15
    RunnicFusion
    0

    Allright,

     

    i've tried the following code:

     

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @if (Model.fotoGallerij.Count() == 1)
      {
        @* <img src ="/ImageGen.ashx?height=500&amp;constrain=true&amp;crop=resize&amp;[email protected]" alt=""> *@
      }
    else if (Model.fotoGallerij.Count() > 1)
       {
        <ul class="image-gallery">
        @foreach (var item in @Model.fotoGallerij)
       {
        <li>
          <a class="gallery grouped" href="/ImageGen.ashx?height=500&amp;constrain=true&amp;crop=resize&amp;[email protected]" title="">
                  
          <img src="/ImageGen.ashx?width=71&amp;height=73&amp;crop=resize&amp;[email protected]" alt=""/></a>
        </li>
       }
      </ul>
     <script>
        $("a.gallery").colorbox({rel:'grouped'});
    </script>
    }

     

    This runs good, but when i uses images in the first if, it give's an error back. (reason of the comment @* and *@)



  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Sep 11, 2012 @ 17:18
    Jeroen Breuer
    0

    That's because you are using @item.image (copied from the loop), but because you are not in the foreach loop that won't work.

    Have you tried:

    <img src ="/ImageGen.ashx?height=500&amp;constrain=true&amp;crop=resize&amp;[email protected]" alt="">

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft