Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Jul 15, 2015 @ 08:43
    Craig O'Mahony
    0

    Loop through content in a Partial View

    Hi folks,

    I'm having trouble looping through content in a partial view. This is what I've done so far:

    @model ICASolution.Models.QualificationsTopViewModel
                            @{
                                var page = umbraco.NodeFactory.Node.GetNodeByXpath("//Qualifications");
                                foreach (var menuItem in page.Children)
                                {
                                    @Html.Raw("<li><a href=\"#\">" + menuItem.Name + "</a></li>");
                                }
                            }
    

    I can see that when the loop is hit that the children are being returned and I can see their properties:

    enter image description here

    But these properties aren't available to me in code:

    enter image description here

    I'm obviously missing something(!) any chance someone could fill me in on where I'm going wrong please?

    thanks, Craig

  • Gary Devenay 39 posts 245 karma points
    Jul 15, 2015 @ 09:21
    Gary Devenay
    101

    Hi Craig,

    I would start by suggesting that you drop the use of umbraco.NodeFactory. This method of retrieving nodes is now deprecated and won't be supported in future versions.

    Instead I would recommend that you use UmbracoHelper with something along the lines of the following:

    Umbraco.Web.UmbracoHelper helper = new Umbraco.Web.UmbracoHelper(UmbracoContext.Current);
    var page = helper.TypedContentSingleAtXPath("//Qualifications");
    
    foreach (var menuItem in page.Children)
    {
        <li>@menuItem.Name</li>
    }
    

    If you check the documentation, you will notice that you can also access other properties from your DocType via the .GetPropertyValue[<T>](string propertyAlias).

    EDIT: The reason that your current implementation won't work is because GetNodeByXPath will always return an IEnumerable (even if there is only one item), hence the use of TypedContentSingleAtXPath above.

    Gary

  • Craig O'Mahony 364 posts 918 karma points
    Jul 15, 2015 @ 09:37
    Craig O'Mahony
    0

    You, my friend, are one sexy mother!

    That's driven me mental

  • 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