Copied to clipboard

Flag this post as spam?

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


  • Phillip Turner 98 posts 412 karma points
    Apr 18, 2014 @ 20:03
    Phillip Turner
    0

    Need help with Where and Contains 6.1.6 Razor

    Good afternoon Umbracians.

    I am trying to get a list of items from my tree where a dropdown field = a certain value.

    I have tried so many different ways and failed. Any advice will be grateful.

    Here is my code:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using System.Xml.XPath
    @using umbraco.MacroEngines
    @{
        int i = 0;
        XPathNodeIterator iterator = umbraco.library.GetPreValues(2309);
        iterator.MoveNext();
        XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
        var centers = CurrentPage.AncestorOrSelf(2).Children;
        List<string> groups = new List<string>();
    
        while (preValues.MoveNext())
        {
            groups.Add(preValues.Current.Value);
        }
    }
    <div id="tab-box-partner" style="background-color: White;">
        @foreach (string group in groups)
        {
            <div class="service-center-block @(i > 0 ? " hidden" : "")">
            <h2>@group</h2>
            <p>
            @foreach (var center in centers.Where(string.Format("serviceCenterType == {0}", group)))
            {
            @(center.serviceCenterName == "" ? center.Name : center.serviceCenterName)
            @center.addressBlock
            }  
            </p>      
        </div>
        i++;
    }
    </div>
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 18, 2014 @ 20:54
    Jeavon Leopold
    0

    Hi Philip,

    It looks like this is a MVC Partial View, could you confirm?

    Jeavon

    Edit: I see you are using Umbraco v6.1.6

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 18, 2014 @ 21:05
    Jeavon Leopold
    0

    Also is your dropdown with property alias "serviceCenterType" a "Dropdown list" or a "Dropdown list, publishing keys"?

  • Phillip Turner 98 posts 412 karma points
    Apr 18, 2014 @ 21:12
    Phillip Turner
    0

    Yes, this is a MVC partial and the DataType is DropDownList.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 18, 2014 @ 21:16
    Jeavon Leopold
    0

    Ok cool, and you want to find all nodes that are the children of the first level 2 node up the tree from the current node that have a certain value selected from that dropdown, e.g. "mything" and loop through them, right?

  • Phillip Turner 98 posts 412 karma points
    Apr 18, 2014 @ 22:03
    Phillip Turner
    0

    Home > Centers > Service Center Address

    Above is the how the tree is.

    I want to get all the nodes under Centers that have a serviceCenterType of XYZ.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 18, 2014 @ 22:12
    Jeavon Leopold
    0

    Ok, unless I'm missing something, I think this should do it?

    @inherits UmbracoTemplatePage
    @{
        var centers = CurrentPage.AncestorOrSelf(2).Children;
        var valueToFind = "XYZ";
    
        var xzy = centers.Where("serviceCenterType == @0", valueToFind);
    
        foreach (var node in xzy)
        {
            <p>@node.Name</p>
        }    
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 18, 2014 @ 22:14
    Jeavon Leopold
    100

    To ignore case, you can change it to:

    var xzy = centers.Where("serviceCenterType.ToLower() == @0", valueToFind.ToLower());
    
  • Phillip Turner 98 posts 412 karma points
    Apr 18, 2014 @ 23:12
    Phillip Turner
    0

    Jeavon to the rescue.

    Worked great.

    @foreach (string group in groups)
    {
        <div class="service-center-block @(i > 0 ? " hidden" : "")">
            <h2>@group</h2>
            @foreach (var center in centers.Where("serviceCenterType == @0", group))
            {
                <p>
                <CenterName>@(center.serviceCenterName == "" ? center.Name : center.serviceCenterName)</CenterName><br />
                <CenterPhone>@center.addressBlock</CenterPhone>
                </p>
            }       
        </div>
        i++;
    }
    
  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 18, 2014 @ 23:18
    Jeavon Leopold
    0

    Great! One additional bit of advice, when using MVC, you should never have @using umbraco.MacroEngines that is from the legacy "Razor Macros" and has no use within MVC templating.

Please Sign in or register to post replies

Write your reply to:

Draft