Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    May 16, 2013 @ 10:53
    Anthony Candaele
    0

    accessing MNTP items with Razor

    Hi,

    I'm struggling here on a piece of Razor code:

    I have a StaffItem document type

    A StaffItem document type has a property, staffCourses, of type MNTP

    Now in in Razor, in a foreach loop through my staff members, I'm trying to list all courses (selected with MNTP) for a staffmember.

    @if (item.HasValue("staffCourses"))

                       {

                           <p><strong>Courses</strong></p>

     

                           foreach (var course in item.staffCourses)

                           {

                              ... print courses

                           }

                       }

    The problem is I can't loop through the staffCourses for my StaffItem. VS Intellisense tells me:

    "umbraco.MacroEngine.DynamicNode doesn't contain a definition for "staffCourses" ..."

    How can I access the MNTP values on my StaffItem doctype ?

    Thanks for your  help,

    Anthony

  • Fuji Kusaka 2203 posts 4220 karma points
    May 16, 2013 @ 11:22
    Fuji Kusaka
    0

    Hi Anthony,

    Try this instead

    if(f.GetProperty("staffCourses").Value !=null && f.Has("staffCourses")){
    }
  • Anthony Candaele 1197 posts 2049 karma points
    May 16, 2013 @ 11:28
    Anthony Candaele
    100

    found the issue,

    The problem was that I defined my collection of staffItem objects as a DynamicNodeList

    DynamicNodeList categorieItems = new DynamicNodeList();
    ...
    foreach (var category in categories)
        {
            categorieItems = @Model.AncestorOrSelf("StaffArea").Children.Where("categoryStaff == \"" + category + "\"");
        }

    I changed my categoryItems to:

    var categorieItems = @Model.AncestorOrSelf("StaffArea")

                .Children.Where("categoryStaff == \"" + category + "\"");

    And now I can loop through the staffCourses on a staffItem like this:

    @if (item.HasValue("staffCourses"))

                       {

                           <p><strong>Courses</strong></p>

                           

                           foreach (var course in item.staffCourses)

                           {

                               ...

                           }

                       }

    Don't know why I keep using this DynamicNodeList, it gives me nothing but trouble.

  • 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