Copied to clipboard

Flag this post as spam?

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


  • Graham Carr 277 posts 389 karma points
    Jul 11, 2014 @ 12:18
    Graham Carr
    0

    Drodpown Dictionary Component Values

    I am using the Drodown Dictionary component created by Matt Brailsford and Lee Kelleher (https://github.com/leekelleher/umbraco-dictionary-dropdown)

    I have it installed and it is working great, however I am struggling trying to work out how to look through the values of the data type via code. For the normal Umbraco dropdown I had this code:

    int categoryDataTypeNodeID = 1057;
    XPathNodeIterator preValueRootElementIterator = umbraco.library.GetPreValues(categoryDataTypeNodeID);
    preValueRootElementIterator.MoveNext(); //move to first
    XPathNodeIterator preValueIterator = preValueRootElementIterator.Current.SelectChildren("value", "");


    Obviously the GetPreValues method does not return the items, so what method should be used? If I look in the cmsPropertyType table the value column is set as follows:

    [ { "key": "Mr", "value": "Mr" }, { "key": "Mrs", "value": "Mrs" }, { "key": "Miss", "value": "Miss" }, { "key": "Ms", "value": "Ms" }, { "key": "Dr", "value": "Dr" }, { "key": "Prof", "value": "Prof" } ]

    This does appear to exist within the TypedValue field when the GetPreValues method is called?

  • Graham Carr 277 posts 389 karma points
    Jul 11, 2014 @ 14:47
    Graham Carr
    0

    Managed to sort it, for future rference the best way of doing this is using the JSON.Net Deserialize method and create a list of a custom class matching the values above, e.g.

            public List<SelectListItem> GetTitleTypes()
            {
                Umbraco.Core.Models.PreValueCollection collection = ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(1333);
                PreValue preval = collection.PreValuesAsDictionary.Values.FirstOrDefault();
                List<keyvalue> values = JsonConvert.DeserializeObject<List<keyvalue>>(preval.Value);
                List<SelectListItem> items = new List<SelectListItem>();

                foreach(keyvalue val in values)
                {
                    SelectListItem item = new SelectListItem();
                    item.Text = val.key;
                    item.Value = val.value;
                    items.Add(item);
                }

                return items;
            }

            class keyvalue
            {
                public string key { get; set; }
                public string value { get; set; }
            }

Please Sign in or register to post replies

Write your reply to:

Draft