Copied to clipboard

Flag this post as spam?

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


  • Robert 33 posts 173 karma points
    Apr 04, 2018 @ 10:56
    Robert
    0

    Getting selected string value of dropdown using IContentService and IContent

    I am working in the backend of umbraco with the IContentService API and I need to read the dropdown selected string value of a combo.

    I have an IContent object representing the object I am working with. I have a dropdown called "Brand" and when I do:

    var brand = content.GetValue

    it does not return the selected string value visible in the UI, rather a number, in this case 231 and I don't what this refers to or how to get the selected string value.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 04, 2018 @ 10:58
    Michaël Vanbrabandt
    0

    Hi Robert,

    can you show us the complete code of where you want to access this Brand property?

    Also which version are you using of Umbraco?

    Thanks!

    /Michaël

  • Robert 33 posts 173 karma points
    Apr 04, 2018 @ 11:00
    Robert
    0

    The code is simply this:

    var brand = content.GetValue("Brand");

    //brand returns 231 but I need the selected string value of the combo

    Where content is the correct IContent object that I want to work with.

    Umbraco 7.

  • Michaël Vanbrabandt 863 posts 3348 karma points c-trib
    Apr 04, 2018 @ 11:08
    Michaël Vanbrabandt
    0

    Hi Robert,

    try this:

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

    Hope this helps!

    /Michaël

  • Robert 33 posts 173 karma points
    Apr 04, 2018 @ 11:07
    Robert
    100

    OK found the answer just need to use umbraco.GetPreValueAsString(231)

  • Nicholas Westby 2054 posts 7104 karma points c-trib
    May 22, 2019 @ 02:09
    Nicholas Westby
    0

    Here's what I did on umbraco 7.14.0:

    /// <summary>
    /// Returns the value of a drop down property.
    /// </summary>
    /// <param name="page">
    /// The page with the drop down property.
    /// </param>
    /// <param name="propName">
    /// The name of the drop down property.
    /// </param>
    /// <returns>
    /// The drop down text value.
    /// </returns>
    private static string GetDropDownValue(IContent page, string propName)
    {
        var dtService = ApplicationContext.Current.Services.DataTypeService;
        var valueId = page.GetValue<int?>(propName);
        if (!valueId.HasValue)
        {
            return null;
        }
        return dtService.GetPreValueAsString(valueId.Value);
    }
    

    That's more just a note to myself in case I search for this again in the future.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies