Copied to clipboard

Flag this post as spam?

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


  • Marcus 6 posts 36 karma points
    Jul 28, 2015 @ 10:14
    Marcus
    0

    Get names of top level items in content

    Hi

    I want a dropdown menu that displays the name of all top level items in content as options. I have tried

    @{
                List<SelectListItem> languages= new List<SelectListItem>();
                //foreach (var language in Model.Content.Siblings())
                foreach (var language in CurrentPage.AncestorOrSelf(1).Siblings())
                {
                    languages.Add(new SelectListItem
                        {
                            Text = language.Name,
                            Value = language.Url
                        });
                }           
                @Html.DropDownList("LanguageSelect",new SelectList(languages,"Value","Text"), htmlAttributes: new { id = "LanguageSelect" });
    }
    

    which obviously doesn't work unless the user is standing at top level. Although out of all options I have looked at here this one is the only one I have gotten to give me something.

    This example pulled from Umbraco.tv

    @foreach (var language in CurrentPage.AncestorOrSelf(1).Siblings())
    {
       languages.Add(new SelectListItem
                        {
                            Text = language.Name,
                            Value = language.Url
                        });
    }
    

    doesn't give me anything. No options in the dropdown at all.

    Help would be much appreciated. Thanks!

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Jul 28, 2015 @ 15:18
    Alex Skrypnyk
    0

    Hi Marcus,

    What version of Umbraco are you using?

  • Marcus 6 posts 36 karma points
    Jul 28, 2015 @ 15:19
    Marcus
    0

    Hello. Umbraco version 7.2.5 assembly: 1.0.5610.18894 :)

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Jul 28, 2015 @ 15:40
    Alex Skrypnyk
    100

    Great ))

    This code works for me :

    @foreach (var node in Umbraco.TypedContentAtRoot())
            {
                @node.Name
            }
    

    Thanks, Alex

  • Marcus 6 posts 36 karma points
    Jul 29, 2015 @ 09:52
    Marcus
    0

    I haven't fully tested it, but it does indeed appear to work!

    Thank you for your help!

  • 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