Copied to clipboard

Flag this post as spam?

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


  • David Tregoning 63 posts 236 karma points
    Feb 14, 2014 @ 11:02
    David Tregoning
    0

    Gettings values from a DropDown list data type

    I am running umbraco version 7 and have created a DropDown list data type called 'productCategories'. I have populated this dropdown list with a number of prevalues and would like to produce a partial view that takes these prevalues and renders them into an unordered list as opposed to a dropdown list. Is this possible?

  • Mike Chambers 635 posts 1252 karma points c-trib
    Feb 14, 2014 @ 12:17
    Mike Chambers
    0

    SortedList values = umbraco.cms.businesslogic.datatype.PreValues.GetPreValues(umbraco.cms.businesslogic.datatype.DataTypeDefinition.GetAll().First(dtDef => dtDef.DataType.DataTypeName.ToString() == "productCategories").DataType.DataTypeDefinitionId);

    You might want to watch for front end use though.. as I think this will always hit the database....

  • David Tregoning 63 posts 236 karma points
    Feb 14, 2014 @ 16:48
    David Tregoning
    0

    Thanks for that Mike, I am new to both Umbraco and .Net but after I posted I further researched and found that the PreValues.GetPreValues was the route to go. However when I used your code the following error appeared:

    System.InvalidOperationException: Sequence contains no matching element
    

    When I use the following code (taking out the reference to the name of the Data Type) where 1964 is the nodeID of my Data type I do not get an error:

    var prevalues = umbraco.cms.businesslogic.datatype.PreValues.GetPreValues(1964);
    foreach (var item in prevalues){
        <p>@prevalues</p>
    }
    

    The output is the correct number of paragraph tags as are prevalues in my data type but they all just say:

    System.Collections.SortedList

    Do you know the way of calling the prevalues into the paragraph tags?

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Feb 14, 2014 @ 17:21
    Andy Butland
    0

    Enumerating a SortedList is not very intuitive - but you can do it like this:

    var prevalues = umbraco.cms.businesslogic.datatype.PreValues.GetPreValues(1964).Values;
    var enumerator = prevalues.GetEnumerator();
    while (enumerator.MoveNext()) 
    {
      <p>@((PreValue)enumerator.Current).Value</p>
    }
    
  • David Tregoning 63 posts 236 karma points
    Feb 17, 2014 @ 10:52
    David Tregoning
    1

    I tried your code Andy but for some reason no paragraph tags were being rendered onto the page (inside the while loop). However I seem to have got what I wanted with this piece of code, not sure if it is correct but helped me and may help others

    @using System.Xml.XPath
    @using umbraco.MacroEngines
    @inherits DynamicNodeContext
    
    @{
        XPathNodeIterator iterator = umbraco.library.GetPreValues(1964);
        iterator.MoveNext();
        XPathNodeIterator preValues = iterator.Current.SelectChildren("preValue", "");
    
        <ul>
        @while (preValues.MoveNext())
        {
            string preValue = preValues.Current.Value;
    
            <li>@preValue</li>                      
        }
        </ul>
    }
    

    Basically this example on the Umbraco website: http://our.umbraco.org/wiki/reference/umbracolibrary/getprevalues

Please Sign in or register to post replies

Write your reply to:

Draft