Copied to clipboard

Flag this post as spam?

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


  • Clare 37 posts 117 karma points
    Dec 16, 2013 @ 15:05
    Clare
    0

    Razor how to list nodes with a certain property (where that property can have multiple values)

    I'm trying to create a macro to list descendants of a particular document type where a property equals a certain value (in this instance the property is ‘scholarship’).

    The property field for ‘scholarship’ in the document type uses an ‘ultimate picker’ and can have more than one option selected.

    I have created other similar macros (using different properties) that have worked where the particular property has used a drop-down list or a text field where only one option is entered for each so I’m guessing that is where the problem is but I’m not sure. The other difference is the property field in this instance uses ‘ultimate picker’ rather than a list.

    This is the what I have at the moment which doesn’t produce a list:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
        string scholarshipToFind = Parameter.ProgrammeScholarship;
        var root =Model.AncestorOrSelf(1);
        var programmeNames = root.DescendantsOrSelf("Programme").OrderBy("programmeName").ToList();
           
           <div>
               <ul class="programme-list">
            @foreach(var item in programmeNames)
            {
                 string[] scholarship = item.scholarship.ToString().Split(',');
               
                if (scholarship.Contains(scholarshipToFind))
                    {
                        <li>
                        <a href="@item.Url">@item.programmeName - @item.qualificationType</a>
                        </li>
                        }
               
                             }
        </ul>
        </div>
               
    }

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 16, 2013 @ 18:05
    Jeavon Leopold
    0

    Hi Clare,

    I would approach this by using a handy Razor helper, something like this:

    @{    
        int scholarshipToFind;
        if (!int.TryParse( Parameter.ProgrammeScholarship, out scholarshipToFind))
        {
            scholarshipToFind = 0;
        }        
    
        var root = CurrentModel.AncestorOrSelf(1);
        var programmeNames = root.DescendantsOrSelf("Programme").Where(x => IdContained(x.GetPropertyValue("scholarship"), scholarshipToFind)).OrderBy(x => x.GetPropertyValue("programmeName"));
    
           <div>
               <ul class="programme-list">
            @foreach(dynamic item in programmeNames)
            {             
            <li>
                <a href="@item.Url">@item.programmeName - @item.qualificationType</a> 
            </li>                         
           }
        </ul>
        </div>
    
    @functions{
    
        private static bool IdContained(string valueCsv, int valueId)
        {
            var idCheckList = valueCsv.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
            return idCheckList.Contains(valueId);
        }
    
    }
    

    Hope that's helpful,

    Jeavon

  • Clare 37 posts 117 karma points
    Dec 17, 2013 @ 10:09
    Clare
    0

    Thanks Jeavon, I tried that but I get an error message when I insert the macro into a page (Error loading MacroEngine script)

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 10:19
    Jeavon Leopold
    0

    Hi Clare,

    Ok, lets add a try/catch so we can see what the error is:

     @{
         try
         {
             int scholarshipToFind;
             if (!int.TryParse(Parameter.ProgrammeScholarship, out scholarshipToFind))
             {
                 scholarshipToFind = 0;
             }
    
             var root = CurrentModel.AncestorOrSelf(1);
             var programmeNames = root.DescendantsOrSelf("Programme").Where(x => IdContained(x.GetPropertyValue("scholarship"), scholarshipToFind)).OrderBy(x => x.GetPropertyValue("programmeName"));
    
             <div>
                 <ul class="programme-list">
                     @foreach (dynamic item in programmeNames)
                     {
                         <li>
                             <a href="@item.Url">@item.programmeName - @item.qualificationType</a> 
                         </li>
                     }
                 </ul>
             </div>
         }
         catch (Exception e)
         {
             <div style="color: red">@e.Message</div>
         }
     }
    
    @functions{
    
        private static bool IdContained(string valueCsv, int valueId)
        {
            var idCheckList = valueCsv.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
            return idCheckList.Contains(valueId);
        }
    
    }
    

    Also the @functions section should be the very last thing in your .cshtml file

    Jeavon

  • Clare 37 posts 117 karma points
    Dec 17, 2013 @ 10:32
    Clare
    0

    Thanks Jeavon, trying that I get the following error:

    Object reference not set to an instance of an object.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 10:51
    Jeavon Leopold
    0

    Is there any other info or only that?

  • Clare 37 posts 117 karma points
    Dec 17, 2013 @ 10:55
    Clare
    0

    When I place the macro on a page that's all I get where the macro is

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 11:40
    Jeavon Leopold
    0

    Hmm, I wonder if there is a problem with the alias of your parameter?

    Maybe it should be Parameter.programmeScholarship?

  • Clare 37 posts 117 karma points
    Dec 17, 2013 @ 12:46
    Clare
    0

    Hi thanks again but still the same, I changed the alias of the parameter to programmeScholarship and changed this in the .cshtml file but still the same result

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 14:10
    Jeavon Leopold
    0

    Strange, seems to work perfectly for me.

    Could you please post your entire .cshtml contents?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 14:27
    Jeavon Leopold
    100

    Ah wait, try updating the function like this (it wasn't checking for no selection in the picker)

    @functions{
    
        private static bool IdContained(string valueCsv, int valueId)
        {
            if (string.IsNullOrEmpty(valueCsv))
            {
                return false;
            }
            var idCheckList = valueCsv.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
            return idCheckList.Contains(valueId);
        }
    
    }
    
  • Clare 37 posts 117 karma points
    Dec 17, 2013 @ 14:30
    Clare
    0
    Here is the content of the .cshtml file. Is it anything to do with the field type for this property being an Ultimate Picker or anything like that?

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
         try
         {
             int scholarshipToFind;
             if (!int.TryParse(Parameter.programmeScholarship, out scholarshipToFind))
             {
                 scholarshipToFind = 0;
             }

             var root = CurrentModel.AncestorOrSelf(1);
             var programmeNames = root.DescendantsOrSelf("Programme").Where(x => IdContained(x.GetPropertyValue("scholarship"), scholarshipToFind)).OrderBy(x => x.GetPropertyValue("programmeName"));

             <div>
                 <ul class="programme-list">
                     @foreach (dynamic item in programmeNames)
                     {
                         <li>
                             <a href="@item.Url">@item.programmeName - @item.qualificationType</a>
                         </li>
                     }
                 </ul>
             </div>
         }
         catch (Exception e)
         {
             <div style="color: red">@e.Message</div>
         }
     }

    @functions{

        private static bool IdContained(string valueCsv, int valueId)
        {
            var idCheckList = valueCsv.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
            return idCheckList.Contains(valueId);
        }

    }

  • Clare 37 posts 117 karma points
    Dec 17, 2013 @ 14:41
    Clare
    0

    I've updated the function now which has removed the error message but I still don't get anything in my list.

    This is now the content of my .cshtml file:

    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    @{
         try
         {
             int scholarshipToFind;
             if (!int.TryParse(Parameter.programmeScholarship, out scholarshipToFind))
             {
                 scholarshipToFind = 0;
             }

             var root = CurrentModel.AncestorOrSelf(1);
             var programmeNames = root.DescendantsOrSelf("Programme").Where(x => IdContained(x.GetPropertyValue("scholarship"), scholarshipToFind)).OrderBy(x => x.GetPropertyValue("programmeName"));

             <div>
                 <ul class="programme-list">
                     @foreach (dynamic item in programmeNames)
                     {
                         <li>
                             <a href="@item.Url">@item.programmeName - @item.qualificationType</a>
                         </li>
                     }
                 </ul>
             </div>
         }
         catch (Exception e)
         {
             <div style="color: red">@e.Message</div>
         }
     }

    @functions{

        private static bool IdContained(string valueCsv, int valueId)
        {
            if (string.IsNullOrEmpty(valueCsv))
            {
                return false;
            }
            var idCheckList = valueCsv.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse);
            return idCheckList.Contains(valueId);
        }

    }

  • Clare 37 posts 117 karma points
    Dec 17, 2013 @ 16:32
    Clare
    0

    It's working now! Sorry it only just clicked that for the macro property I needed to enter the ID of the page rather than the value.

    Thank you so much for your help

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Dec 17, 2013 @ 17:08
    Jeavon Leopold
    0

    That's great! It's a really tidy way to handle this type of common requirement to filter based on CSV pickers like Ultimate and Multinode pickers.

Please Sign in or register to post replies

Write your reply to:

Draft