Copied to clipboard

Flag this post as spam?

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


  • bparks 23 posts 79 karma points
    Oct 18, 2013 @ 11:03
    bparks
    0

    Getting integer values from prevalue list

    I'm writing a user control (web forms) that creates a dropdownlist -- the dropdown list needs to get its keys/values from a dropdown list data type that I've created in Umbraco.  The Umbraco dropdown data type is using integer.

    I'm able to get the strings for the dropdown by :

       var categoryType = service.GetAllDataTypeDefinitions().FirstOrDefault(x=>x.Name== "Visitor Category");
       var prevalues = service.GetPreValuesByDataTypeId(categoryType.Id);

    But, I'm not sure how to get the integer value for each prevalue, since "GetPreValuesByDataTypeId" only returns an array of strings.

    I'm using 6.0.5.

    Thanks

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Oct 20, 2013 @ 10:22
    Lars-Erik Aabech
    0

    All of that stuff is stored as text in the database, and as far as I know it's up to you handle the "serialization".

    You can easily convert it to integers yourself:

    var intValues = prevalues.Select(s => Convert.ToInt32(s)).ToArray();
    

    Lars-Erik

  • bparks 23 posts 79 karma points
    Oct 21, 2013 @ 07:27
    bparks
    1

    Sorry, I think you misunderstood my question (or I didn't state it clearly):

    If I have a dropdown list with prevalues as follows:
    Text="Teacher", Value=11
    Text="Student", Value=12 ... and so on...

    The call to GetPreValuesByDataTypeId only returns a string array with ["Teacher", "Student"]... not the corresponding integer values.  If I'm constructing that dropdown list in code (from my own user control), I need both the text and value.

    Thanks for your response.

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Oct 21, 2013 @ 10:36
    Lars-Erik Aabech
    1

    Oh bugger, what a silly unprepared answer I gave you. Sorry! :)

    I'm out of time and memory at the moment, but here's a few pointers:

    If you set the prevalue editor of your datatype to be a KeyValuePrevalueEditor, you'll get access to both values. The property will have the ID of the selected value(s) when you use it.

    Here's a bit of code:

    public class SomeDataType : BaseDataType, IDataType
    {
        //...
        public override IDataEditor DataEditor
        {
            get
            {
                if (editor == null)
                    editor = new SomeEditor(Data, ((KeyValuePrevalueEditor)PrevalueEditor).PrevaluesAsKeyValuePairList);
                return editor;
            }
        }
        //...
        public override IDataPrevalue PrevalueEditor
        {
            get
            {
                if (prevalueeditor == null)
                    prevalueeditor = new KeyValuePrevalueEditor(this);
                return prevalueeditor;
            }
        }
        //...
    }
    
    public class SomeEditor : DropDownList, IDataEditor
    {
        //...
        public SomeEditor(IData data, List<KeyValuePair<int, string>> prevalues)
        {
            this.data = data;
            this.prevalues = prevalues;
        }
        //...
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            if (prevalues != null)
            {
                foreach (KeyValuePair<int, string> keyValuePair in prevalues)
                    Items.Add(new ListItem(keyValuePair.Value, keyValuePair.Key.ToString()));
            }
            //...
        }
    }
    
  • Jo 1 post 71 karma points
    Feb 12, 2016 @ 17:33
    Jo
    0

    A bit late to the party here, but this took a long time to dig up from the internet... This is for version 7.1.6

    I am attempting to import document type(s) from an Umbraco package (package.xml). CheckBoxLists (and probably other types) provide selected items as text, but the cmsPropertyData table requires the IDs.

    Looking at only Umbraco.CheckBoxLists at the moment, I used the following back end C# code to obtain a "," delimited string of selected ids.

    The preValues you get, are an array of keys and values, where the value contains both the Id and the Value.

    var contentService = ApplicationContext.Current.Services.ContentService;
    var dataTypeService = ApplicationContext.Current.Services.DataTypeService;
    
    if (genericProperty.Type.InvariantEquals("Umbraco.CheckBoxList"))
    {
        // obtain the data type definition (we need the nodeId, as that too is not in the xml)
        var dataTypeDefinition = dataTypeService.GetDataTypeDefinitionById(new Guid(genericProperty.Definition).Value));
    
        // get pre values (ids / values) - [0, Umbraco.CoreModels.PreValue] where PreValue: (Key, Value) - Value.Id, Value.SortOrder, Value.Value
        var preValues = dataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeDefinition.Id).PreValuesAsDictionary;
    
        // get "," delimited string of ids where .Value is in stringValue
        var value = String.Join(",", preValues.Where(v => stringValue.Contains(v.Value.Value)).Select(i => i.Value.Id).ToArray());
    }
    

    genericProperty - I obtained from Umbraco's package.xml export, for example:

    <GenericProperty>
      <Name>Favourite Colours</Name>
      <Alias>favouriteColours</Alias>
      <Type>Umbraco.CheckBoxList</Type>
      <Definition>23e3859e-b11d-45f6-a8c2-34ac95b6ddc8</Definition>
      <Tab>Content</Tab>
      <Mandatory>False</Mandatory>
      <Validation />
      <Description><![CDATA[]]></Description>
    </GenericProperty>
    

    stringValue - the text(s) for the id(s) I need, for example:

    <favouriteColours><![CDATA[Red,Purple,Orange]]></favouriteColours>
    

    Jo

Please Sign in or register to post replies

Write your reply to:

Draft