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
    Apr 25, 2012 @ 11:21
    Anthony Candaele
    0

    problem with filtering nodes in Razor

    Hi,

    For a navigation menu, I want to filter research staff members that belong to a certain category (professor, teaching assistant, practicing assistent, etc...)

    Therefor I have a property 'memberCategory' of datatype dropdown list on my 'member' documenttype:

     

    <Member id="1071" parentID="1065" level="3" writerID="0" creatorID="0" nodeType="1063" template="1069" sortOrder="3" createDate="2012-04-24T10:36:22" updateDate="2012-04-24T11:04:58" nodeName="John Doe" urlName="john-doe" writerName="admin" creatorName="admin" path="-1,1051,1065,1071" isDoc="">

            <memberFirstName>John</memberFirstName>

            <memberLastName>Doe</memberLastName>

            <memberEmail>[email protected]</memberEmail>

            <memberPhone>some phonenumber</memberPhone>

            <memberFax>some faxnumber</memberFax>

            <memberAddress><![CDATA[Umbraco University, Department of Razor Sciences

    addres, postal number city, country]]></memberAddress>

            <memberVisitingHours><![CDATA[]]></memberVisitingHours>

            <memberBio><![CDATA[

    <p>Bio information</p>

    ]]></memberBio>

            <memberPublications />

            <memberCategory><![CDATA[Professor]]></memberCategory>

            <pageTitle>John Doe</pageTitle>

            <pageDescription><![CDATA[Details Prof. Dr. John Doe]]></pageDescription>

            <umbracoNaviHide>0</umbracoNaviHide>

          </Member>

    My Razor script code looks like this:

    @using umbraco.MacroEngines

    @inherits umbraco.MacroEngines.DynamicNodeContext

     

    @{DynamicNodeList professors = @Model.Member.Children.Where("memberCategory == \"Professor\"");}

     

        @if (professors.Items.Any())

         {

           <h2>Professor</h2>

             <ul class="nav">

               @foreach (DynamicNode professor in professors)

                {        

     

                    <li><a href="@professor.Url">@professor.Name</a></li>

     

                }

              </ul> 

         }

    However, this is not working, what am I doing wrong?

    Thanks for your help,

    Anthony

  • gilad 185 posts 425 karma points
    Apr 25, 2012 @ 11:29
    gilad
    1

    Hi Anthony

    Why you are using - @Model.Member.Children.Where("memberCategory == \"Professor\"");}?

    Try this - @Model.Children.Where("memberCategory == \"Professor\"");} ( without 'Member' )

    Hope that help.

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 25, 2012 @ 11:32
    Anthony Candaele
    0

    Hi gilad,

    Again you come to the rescue :)

    Yup, that was the problem, and you solved it.

    greetings,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 25, 2012 @ 11:43
    Anthony Candaele
    0

    This is the complete code

    @using umbraco.MacroEngines

    @inherits umbraco.MacroEngines.DynamicNodeContext

                      

    @{DynamicNodeList professors = @Model.Children.Where("memberCategory == \"Professor\"");}

    @{DynamicNodeList teachingassistants = @Model.Children.Where("memberCategory == \"Teaching Assistant\"");}

        

        @if (professors.Items.Any())

         {

           <h2>Professor</h2>

             <ul class="nav">

               @foreach (DynamicNode professor in professors)

                {        

                 

                    <li><a href="@professor.Url">@professor.Name</a></li>

                         

                }

              </ul> 

         }

         

         @if (teachingassistants.Items.Any())

         {

             <h2>Teaching Assistants</h2>

             <ul class="nav">

                 @foreach (DynamicNode teachingassistant in teachingassistants)

                 {

                     <li><a href="@teachingassistant.Url">@teachingassistant.Name</a></li>

                 }

             </ul>

         }

    and the result looks like this:

     

    Also I checked the very instructive video by Douglas Robar on how to use Umbraco Razor Intellisense and Debugging:

    http://blog.percipientstudios.com/2011/3/3/how-to-umbraco-razor-intellisense-and-debugging.aspx

    Hope this might help other developers as well,

    Anthony

  • gilad 185 posts 425 karma points
    Apr 25, 2012 @ 12:11
    gilad
    1

    Hi Anthony.

    Great to know that it is work for you.

    if you have more categories and you dont want to make it mannualy for each one

    you can also make something like this : 

    List<string> categories = new List<string> { "Professor", "Teaching Assistant" }; //etc...
        DynamicNodeList categoryItems;
        foreach (var category in categories)
        {
            categoryItems = @Model.Children.Where("memberCategory == \"" + category + "\"");
            if (categoryItems.Items.Any())
            {
               <h2>@category</h2>
                 <ul class="nav">
                   @foreach (DynamicNode item in categoryItems)
                   {        
                        <li><a href="@item.Url">@item.Name</a></li>
                   }
                  </ul> 
            }
        }


  • Anthony Candaele 1197 posts 2049 karma points
    Apr 25, 2012 @ 14:13
    Anthony Candaele
    0

    Hi gilad,

    This code is indeed a lot more cleaner and nice. I implemented it, and everything works fine.

    The only issue I have to solve yet, is that when you select an individual member in the navigation menu, the navigation menu disappears because the Razor code traverses only childnodes.

    My content tree looks like this:

    Do you know how to list all member nodes, even when I'm on a single member page?

    Thanks for your help,

    Anthony

Please Sign in or register to post replies

Write your reply to:

Draft