Im trying to use the datalist in a couple of places, but i need to render the "name" from the json, and not the value. Eg, from the country list i need to render "Afghaistan", not "AF". How do i do this in front end Razor?
@foreach (var item in Model.Value<IEnumerable<string>>("countries")){
<span>@item.Value("name"),</span>
}
Only the "value" part is stored for the property's value. Some of the Data List sources do have built-in value-converter (e.g. to change the value back to it's original object). But the countries one doesn't (currently) do that.
To get the country's name from the ISO code, you can use the RegionInfo class, like so...
@foreach (var item in Model.Value<IEnumerable<string>>("countries"))
{
var country = new System.Globalization.RegionInfo(item);
<span>@country.DisplayName,</span>
}
I'd considered a few ways of doing it, but would have required a big dev change - and given that no one else has mentioned this since its release (apart from you today 😊), I had put it down to an edge-case.
In situations when I've needed to use the value's label on the frontend, I've opted to use the "Umbraco Content" data-source, and have content nodes for the options. Sure, it adds to the content-tree structure, but also gives flexibility of a content-editor being able to manage the options/values.
Could that be an appropriate alternative solution?
The Umbraco Content data-source will return the "value" as an IPublishedContent object - basically what Umbraco would gives you for a page in a Razor view.
An example code snippet would be...
@foreach (var item in Model.Value<IEnumerable<IPublishedContent>>("alias"))
{
<span>@item.Name</span>
}
On a side note...
About the data-source, I can appreciate that it can be confusing as to why some convert the value to an object and others are the raw string value. The main reason is me, as in, when I develop them, I make a judgement call of what a common use-case would be. So for data-sources like counties, I'd assumed people would use the ISO code value; for the Umbraco content one, people would assume an IPublishedContent.
Get "name" from datalist
Hi
Im trying to use the datalist in a couple of places, but i need to render the "name" from the json, and not the value. Eg, from the country list i need to render "Afghaistan", not "AF". How do i do this in front end Razor?
Doesn't work
Hi Claus,
Only the "value" part is stored for the property's value. Some of the Data List sources do have built-in value-converter (e.g. to change the value back to it's original object). But the countries one doesn't (currently) do that.
To get the country's name from the ISO code, you can use the
RegionInfo
class, like so...Hope this helps?
Cheers,
- Lee
Hi Lee
It does on the country one. Great!
But i have some that are "user defined lists"... Am i screwed here?
"screwed" isn't the word I'd use. 😆 but yes, with the user-defined data-sources, only the "value" is available on the frontend.
Last year Chriztian, needed something similar... https://our.umbraco.com/packages/backoffice-extensions/contentment/contentment-feedback/102742-how-can-i-output-the-label-of-a-datalist-item
I'd considered a few ways of doing it, but would have required a big dev change - and given that no one else has mentioned this since its release (apart from you today 😊), I had put it down to an edge-case.
In situations when I've needed to use the value's label on the frontend, I've opted to use the "Umbraco Content" data-source, and have content nodes for the options. Sure, it adds to the content-tree structure, but also gives flexibility of a content-editor being able to manage the options/values.
Could that be an appropriate alternative solution?
Wow im really getting into unknown areas here...
How do i get content from a node UDI?
Using the "umbraco content" data source returns something like
No worries, we'll get there. 😃
The Umbraco Content data-source will return the "value" as an
IPublishedContent
object - basically what Umbraco would gives you for a page in a Razor view.An example code snippet would be...
On a side note...
Hope this helps?
It did :)
is working on a reply...