Copied to clipboard

Flag this post as spam?

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


  • Jim 18 posts 36 karma points
    Nov 29, 2010 @ 16:10
    Jim
    0

    Document.GetProperty on dropdown datatype

    Hi All;

    I am trying to get the text value of a dropdown list from the API and I am seriously struggling.

     

    This is what I have at the moment:

    Document doc = new Document(Node.GetCurrent().Id);
    doc.GetProperty("fieldPropertyName").Value;

     

    This returns a string representation of the id for the prevalue.

    What I want is the text for that pre value.

    Thanks in advance for the help.

     

    Jim

  • Patrik 37 posts 89 karma points
    Feb 18, 2011 @ 08:40
    Patrik
    0

    Nobody knows?

    I'm stuck with this problem as well! :/

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Feb 18, 2011 @ 09:05
    Hendy Racher
    0

    Hi Patrik,

    You should be able to get at the dropdown text value by using the Node.GetProperty() as it's the text value that's pushed to the XML, whilst the database stores the associated ID - hence the Document.GetProperty returns this.

    It's also possible to get the text value from the Id, via the umbraco.cms.businesslogic.PreValues object. I've not tested the following, but hopefully should point you in the right direction:

    int dataTypeId = ??;
    int dropDownId = ??;
    string dropDownText = string.Empty;

    SortedList preValues = PreValues.GetPreValues(dataTypeId);
    PreValue preValue = preValues.Values.Cast<PreValue>().Where(x => x.Id == dropDownId).FirstOrDefault();

    if (preValue != null)
    {
    dropDownText = preValue.Value;
    }

    Cheers,

    Hendy

  • Calvin Frei 106 posts 314 karma points
    Feb 18, 2011 @ 09:15
    Calvin Frei
    1

    Or you can try: umbraco.library:GetPreValueAsString(int Id);

  • Patrik 37 posts 89 karma points
    Feb 18, 2011 @ 09:19
    Patrik
    0

    Thanks alot guys, solved my issue!

  • Hendy Racher 863 posts 3849 karma points MVP 2x admin c-trib
    Feb 18, 2011 @ 10:57
    Hendy Racher
    0

    Hi Calvin,

    That's a much better / simpler answer :)

  • Barry Fogarty 493 posts 1129 karma points
    Aug 18, 2011 @ 17:49
    Barry Fogarty
    0

    Apologies for dragging up an old thread, I also have this issue.  I have tried every combo of the dropdown list options (publishing keys, storing as nvarchar, int etc) but it always returns the ID (dropdown key) as a string, rather than the value.  I have followed Calvin's advice and it works (thanks mate!) but it seems like a palaver to have to follow the below process:

    1. Convert string ID to int and ensure it is valid int. 
    2. Utilise the umbraco.library.GetPreValueAsString(int Id) method to return the dropdown value as a string.

    I have no need of the ID on the site so it seems daft you cannot set it to output the value by default.  FYI I am using this datatype as a property of a member type, I have not checked if it is the same used with a doctype.

     

Please Sign in or register to post replies

Write your reply to:

Draft