Copied to clipboard

Flag this post as spam?

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


  • Keith Hubbard 174 posts 402 karma points
    Oct 13, 2014 @ 13:35
    Keith Hubbard
    0

    List children of a content ID with MVC

    I want to list all child images and link of GalleyCatagorie on the homepage. before with dynamic node i would have used NodebyID. The example below is from dynamic node, I want to know how to do this with MVC.

        @{
    
        <div class="FeaturedProduct">
        @foreach (var group in @Model.NodeById(2045).Children.InGroupsOf(3))
        {
           foreach(var item in group)
            {
                // find you whether is the first item of the group and set the css class
                var itemClass = item.IsFirst("first item left", "item left");
    
                // If there is not photo, fallback to the placeholder
                var src = @item.Cropper ?? "/images/Placeholders/photo_placeholder.gif";
                <div class="image">
                 @if (Model.HasValue("Cropper"))
    {
        <img src="@Model.Cropper.Find("@name", "250").url" /><br />
    
    }
                </div>            
            }
    
            // Clear after each group of 3
            <div class="clearBoth"> </div>
        }
        </div>
    }
    
  • Dan Lister 416 posts 1973 karma points c-trib
    Oct 13, 2014 @ 13:53
    Dan Lister
    0

    Hi Keith,

    I think something like the following should work...

    @foreach (var group in Umbraco.TypedContent(2045).Children.InGroupsOf(3))
    {
    }
    

    Thanks, Dan.

  • Keith Hubbard 174 posts 402 karma points
    Oct 13, 2014 @ 14:20
    Keith Hubbard
    0

    Here is the updated code but i do not get images or links it shows the divs

    @{

    <div class="FeaturedProduct">
        @foreach (var group in Umbraco.TypedContent(1101).Children.InGroupsOf(3))
        {
            foreach (var item in group)
            {
                // find you whether is the first item of the group and set the css class
                var itemClass = item.IsFirst("first item left", "item left");
    
                // If there is not photo, fallback to the placeholder
                var src = @item.GetCropUrl("cropper", "thumbnail");
                <div class="image">
                    @if (Model.Content.HasValue("cropper"))
                    {
                        <img src="@item.Url" alt="@item.Name" /><br />
                        <h3><a href="@item.Url">@item.Name</a></h3>
                    }
                </div>
            }
    
            // Clear after each group of 3
            <div class="clearBoth"> </div>
        }
    </div>
    

    }

  • Dan Lister 416 posts 1973 karma points c-trib
    Oct 13, 2014 @ 14:38
    Dan Lister
    0

    Hi Keith,

    I think your if statement is using the wrong object and your image is using the wrong url. Instead of Model, shouldn't it use item?

    if (item.HasValue("cropper"))
    {
        <div class="image">
            <img src='@item.GetCropUrl("cropper", "thumbnail")' alt="@item.Name" /><br />
            <h3><a href="@item.Url">@item.Name</a></h3>
        </div>
    }
    

    Thanks, Dan.

  • Keith Hubbard 174 posts 402 karma points
    Oct 15, 2014 @ 05:53
    Keith Hubbard
    0

    no need for a if statement.

  • 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