Developer forum

Forum » Development » Get dropdownlist label

Get dropdownlist label

Lars Larsen
Lars Larsen
Reply

Hi

I use Dynamicweb.Services.Items.GetItemByPageId(pageid, false) to get an item. This item has a dropdown list field and I want to get both its selected value and label. How can I get the label?


Replies

 
Alexey Tanchenko Dynamicweb Employee
Alexey Tanchenko
Reply

Hello!

Please look at this example.

Usings:

using Dynamicweb.Content.Items;
using Dynamicweb.Content.Items.Metadata;

Method:

public static string RenderOptionsExample(int itemPageId, string itemFieldSystemName)
{
    StringBuilder result = new StringBuilder();
    Item item = Services.Items.GetItemByPageId(itemPageId, false);
    FieldMetadataCollection itemFields = ItemManager.Metadata.GetItemFields(item?.SystemName);
    ItemField field = itemFields.Find(itemFieldSystemName);
    string itemValue = item[itemFieldSystemName].ToString();
    FieldOptionSetupMetadata setup = field.Options;
 
    if (setup.Values.Count > 0)
    {
        result.Append("<ul>");
        foreach (FieldOptionMetadata option in setup.Values)
        {
            bool selected = itemValue.Equals(option.Value.ToString());
            string style = selected ? " style='font-weight: bold;'" : string.Empty;
 
            result.Append($"<li {style}>");
            result.Append($"{option.Name} ({option.Value})");
            result.Append("</li>");
        }
        result.Append("</ul>");
    }
 
    return result.ToString();
}

 

 

You must be logged in to post in the forum