Copied to clipboard

Flag this post as spam?

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


  • Lewis Smith 211 posts 620 karma points c-trib
    Mar 20, 2018 @ 12:19
    Lewis Smith
    0

    Dropdown list multiple

    Hi all,

    I'm trying to loop through a multiple list dropdown and I'm having issues.

    Everywhere online states that the returned value is a comma-separated list so I assumed a string would be returned which I could then loop through using a .Split(',') but this doesn't seem to work.

    If i do this i get the following outputs:

    System.Linq.Enumerable+WhereSelectArrayIterator`2[System.String System.String]

    I have followed the tutorial on http://www.computermagic.gr/tutorials/umbraco-7/property-editors/dropdown-list-multiple/

    Here is the code i have:

    var test = item.GetPropertyValue<string>("ringCategory");
    foreach (var cat in test.Split(','))
    {
        <p>@cat</p>
    }
    

    I have also tried casting the item to an array thinking this may be an issue by both declaring the item as a

    Any help?!

    Lewis

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 20, 2018 @ 12:57
    Dennis Aaen
    0

    Hi Lewis,

    Can you try to use something like this:

    @{
      if (Model.Content.HasValue("ringCategory")){                                                     
           <ul>                                                        
          @foreach(var item in Model.Content.GetPropertyValue<string>("ringCategory").Split(',')) { 
            <li>@item.Name</li>
          }
        </ul>                                                                                       
      }
    } 
    

    Please keep in mind that you change it so it match your case.

    Hope this helps,

    /Dennis

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Mar 20, 2018 @ 13:03
    Dan Diplo
    1

    What version of Umbraco are you using?

    In theory you should be able to just do this:

    var values = GetPropertyValue<IEnumerable<string>>("ringCategory");
    

    This returns an array of strings.

    However, when I tried this in Umbraco 7.9.2 the collection was always empty. I think there must be a bug in this version.

    If you check your umbraco.config file in /App_Data/ can you see your ringCategory property on any of your documents?

  • Proxicode 128 posts 324 karma points
    Nov 20, 2018 @ 22:12
    Proxicode
    1

    Thanks Dan! I'm in 7.12.1 and it works as you described.

    if (Model.Content.HasValue("filterVals"))
    {
        var filterValues = Model.Content.GetPropertyValue<IEnumerable<string>>("filterVals");
        if(filterValues != null && filterValues.Any())
        {
            foreach(var item in filterValues)
            { 
                <dd class="label-sm">@item</dd>
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft