Inx
Inx

Reputation: 2384

How to get the string value of a prevalue in umbraco

I have added a few prevalues to a dropdown-list. When Im trying to get the value of a property that uses my dropdownlist-data-type I keep getting the "key" of the preValue as the value of the property.. but thats not what Im after.. I need and want to get the actuallt prevalue-string that I entered as my prevalue..

Any ideas?

Oh and btw.. I need to do it using C#..

EDIT

This is what I have tried so far.. and it sort of works.. but It retrives a list of lists that I cant do any quering against..

PreValues.GetPreValues(new Document(statistic.ParentId,true).getProperty("identifier").PropertyType.DataTypeDefinition.DataType.DataTypeDefinitionId).Values

Upvotes: 4

Views: 8844

Answers (4)

Alex
Alex

Reputation: 2915

I didn't need it for drop down list. From Umbraco documentation:

Typed:

@if (Model.Content.HasValue("superHero"))
{
    <p>@Model.Content.GetPropertyValue("superHero")</p>
}

Dynamic:

@if (CurrentPage.HasValue("superHero"))
{
    <p>@CurrentPage.superHero</p>
} 

However, for radiolist, the prevalue can be accessed as follows:

Typed:

@if (Model.Content.HasValue("miniFigure"))
{
    var preValue = Umbraco.GetPreValueAsString(Model.Content.GetPropertyValue<int>("miniFigure"));
    <p>@preValue</p>
}

Dynamic:

@if (CurrentPage.HasValue("miniFigure"))
{
    var preValue = Umbraco.GetPreValueAsString(CurrentPage.miniFigure);
    <p>@preValue</p>
}   

Upvotes: 1

Dan S&#248;rensen
Dan S&#248;rensen

Reputation: 25

Using Umbraco version 7.53, I just did this, to get the dropdown value:

string dropdownTextValue = slider.GetProperty("dropdownAlias").DataValue.ToString();

Upvotes: 0

Nishant Kumar
Nishant Kumar

Reputation: 6083

I am using below approch to get dropdown list value as text and id

// Node ID of content node in which dropdown has been used 
int NodeIdOfContent = 1111; 
Document docMfsFeedSettings = new Document(NodeIdOfContent); 

int preValueID = docMfsFeedSettings.getProperty("dropDownAliasName").Value)

// Retrieve Text value 
string textValue = umbraco.library.GetPreValueAsString(
                  Convert.ToInt32(docMfsFeedSettings.getProperty("dropDownAliasName").Value));    

Upvotes: 5

Douglas Ludlow
Douglas Ludlow

Reputation: 10922

It's kind of a mess, but using the PreValues object here's what you would do to get an IEnumerable of just the values:

...

int id = statistic.ParentId;
var node = new Document(id, true);
var property = node.getProperty("dropdown");
var dataTypeDefinitionId = property.PropertyType.DataTypeDefinition.DataType.DataTypeDefinitionId;

// Cast the SortedList to an IEnumerable of DictionaryEntry and then
// select the PreValue objects from it:
var preValues = PreValues.GetPreValues(dataTypeDefinitionId)
    .Cast<DictionaryEntry>()
    .Select(d => d.Value as PreValue);

// Select just the values from the PreValue objects:
var values = preValues.Select(p => p.Value);

Another approach would be to use the umbraco.library.GetPrevalues(int id) method. However both approaches are messy in their own ways.

Upvotes: 2

Related Questions