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.
/// <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.
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.
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
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.
Hi Robert,
try this:
Hope this helps!
/Michaël
OK found the answer just need to use umbraco.GetPreValueAsString(231)
Here's what I did on umbraco 7.14.0:
That's more just a note to myself in case I search for this again in the future.
is working on a reply...