Copied to clipboard

Flag this post as spam?

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


  • Jan Bengtsson 64 posts 108 karma points
    Jul 11, 2016 @ 11:15
    Jan Bengtsson
    0

    How to get prevalues from DTP List Picker

    Hi,

    I'm using the DTP List Picker frequently in Umbraco projects because of the possibility to set custom values. However in my latest project I also need to get the prevalues - text and value - programmatically in an mvc controller. To do that with a standard Umbraco radiobuttonlist, for example, is quite straightforward, but the DTP List Picker beats me. I've tried to serialize and deserialize the json back and forth but without luck. Is there anyone having succeeded with this and kind enough to provide some sample code?

    /Jan

  • Ian 178 posts 752 karma points
    Jul 11, 2016 @ 18:36
    Ian
    0

    Is this any different from what you already have? What does the json you are trying to deserialize look like?

        IContentType currentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(CurrentPage.DocumentTypeAlias);
    PropertyType currentPropertyType = currentContentType.PropertyTypes.Where(pt => pt.Alias == "YOURPROPERTYALIAS").FirstOrDefault();
    var preValues = ApplicationContext.Current.Services.DataTypeService.GetPreValuesByDataTypeId(currentPropertyType.DataTypeDefinitionId);
    
  • Jan Bengtsson 64 posts 108 karma points
    Jul 12, 2016 @ 07:50
    Jan Bengtsson
    0

    Thanks for your interest in my problem, Ian.

    I'm fetching the prevalues just like you do - by using the DataTypeServices GetPreValuesByDataTypeId method.

    The content of the preValues variable is, according to Visual Studio's Text Visualizer:

    {
      "type": "radio",
      "items": [
        {
          "checked": false,
          "text": "Women",
          "value": "1"
        },
        {
          "checked": false,
          "text": "Men",
          "value": "2"
        },
        {
          "checked": false,
          "text": "Young",
          "value": "3"
        },
        {
          "checked": false,
          "text": "Unisex",
          "value": "4"
        }
      ]
    }
    

    It's when trying to deserialize this json into a C# list, from where I can take it further, I fail. Can you help?

    /Jan

  • Jan Bengtsson 64 posts 108 karma points
    Jul 20, 2016 @ 07:14
    Jan Bengtsson
    0

    Hi Jason,

    I address myself directly to you since you, as the project owner, should be able to help me on this matter, if any.

    I need to bring up the text and value properties from a number of DTP List Picker datatypes. But I don't seem to be able to deserialize the json correctly in my Mvc Controller (see postings above).

    I would be most thankful if you take the time to show me the working code.

    /Jan

  • Ian 178 posts 752 karma points
    Jul 20, 2016 @ 13:38
    Ian
    1

    Tested this and it works, no null checks but something to build on

    Class 1

    public class DTPPreValue
    {
        [JsonProperty(PropertyName = "type")]
        public string Type { get; set; }
        [JsonProperty(PropertyName = "items")]
        public List<DTPItem> Items {get; set;}
    }
    

    Class 2

    public class DTPItem
    {
        [JsonProperty(PropertyName = "checked")]
        public bool Checked { get; set; }
        [JsonProperty(PropertyName = "text")]
        public string Text { get; set; }
        [JsonProperty(PropertyName = "value")]
        public string Value { get; set; }
     }
    

    View

    IContentType currentContentType = ApplicationContext.Current.Services.ContentTypeService.GetContentType(CurrentPage.DocumentTypeAlias);
    PropertyType currentPropertyType = currentContentType.PropertyTypes.Where(pt => pt.Alias == "YOURPROPERTYALIAS").FirstOrDefault();
    var preValuesString = ApplicationContext.Current.Services.DataTypeService.GetPreValuesByDataTypeId(currentPropertyType.DataTypeDefinitionId).FirstOrDefault();
    DTPPreValue preValue = JsonConvert.DeserializeObject<DTPPreValue>(preValuesString);
    

    Type property could also be made into an enum on your model.

    If this doesn't work for you I suggest there must be some other issue with your project, if so maybe try re-installing the package.

  • Jan Bengtsson 64 posts 108 karma points
    Jul 22, 2016 @ 06:54
    Jan Bengtsson
    0

    It works fine. Many thanks for the help. /Jan

Please Sign in or register to post replies

Write your reply to:

Draft