Copied to clipboard

Flag this post as spam?

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


  • Johannes Giske 8 posts 121 karma points
    Mar 30, 2016 @ 14:19
    Johannes Giske
    0

    Getting the correct value from a dropdown

    I'm having a hard time accessing the value of a simple dropdown.

    enter image description here

    The dropdown has 2 prevalues, defined in the data type:

    enter image description here

    However, when i try to acces the selected value using this fairly simple code it returns a number (an ID?) and not the selected value.

    var theme = content.GetValue<string>("theme");
    

    Example from debugging: (Dark is selected here)

    enter image description here

    What am I doing wrong?

  • Steve Morgan 1346 posts 4453 karma points c-trib
    Mar 30, 2016 @ 15:04
    Steve Morgan
    0

    What is content - how is this passed, are you in a macro / view / partial?

    With an IPublishedContent object this is working for me:

    Model.GetPropertyValue<string>("BlockTextPosition")
    
  • Johannes Giske 8 posts 121 karma points
    Mar 31, 2016 @ 07:45
    Johannes Giske
    102

    Hi Steve :)

    content is an IContent, and I'm accessing it in a class which handles events from the ContentService

    At the moment I'm subscribing to the ContentService.Saved event.

     private void OnContentSaved(IContentService sender, SaveEventArgs<IContent> saveEventArgs)
    

    It seems that IContent does not have the method GetPropertyValue<T>.

    However, when going over my code again, I found that I've already encountered this problem some time ago and fixed it then

    umbraco.library.GetPreValueAsString(Convert.ToInt32(theme));
    
  • Steve Morgan 1346 posts 4453 karma points c-trib
    Mar 31, 2016 @ 08:34
    Steve Morgan
    0

    Hi,

    Yes - that will work!

    I think the umbraco.library stuff is deprecated - probably should be using the DataType Service

    https://our.umbraco.org/documentation/reference/Management/Services/DataTypeService

    But it works, so it's up to you to break it :)

    Steve

Please Sign in or register to post replies

Write your reply to:

Draft