Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 936 posts 2571 karma points
    Nov 15, 2021 @ 08:59
    Claushingebjerg
    0

    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?

    @foreach (var item in Model.Value<IEnumerable<string>>("countries")){
    <span>@item.Value("name"),</span>
    }
    

    Doesn't work

    enter image description here

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 15, 2021 @ 11:08
    Lee Kelleher
    101

    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...

    @foreach (var item in Model.Value<IEnumerable<string>>("countries"))
    {
        var country = new System.Globalization.RegionInfo(item);
        <span>@country.DisplayName,</span>
    }
    

    Hope this helps?

    Cheers,
    - Lee

  • Claushingebjerg 936 posts 2571 karma points
    Nov 15, 2021 @ 11:15
    Claushingebjerg
    0

    Hi Lee

    It does on the country one. Great!

    But i have some that are "user defined lists"... Am i screwed here? enter image description here

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 15, 2021 @ 11:26
    Lee Kelleher
    0

    "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?

  • Claushingebjerg 936 posts 2571 karma points
    Nov 15, 2021 @ 14:02
    Claushingebjerg
    0

    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

    umb://document/2ce53b1f9aa44e6b88c78e0aa24235a1
    
  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Nov 15, 2021 @ 14:16
    Lee Kelleher
    1

    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...

    @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.

    Hope this helps?

  • Claushingebjerg 936 posts 2571 karma points
    Nov 15, 2021 @ 14:24
    Claushingebjerg
    1

    It did :)

Please Sign in or register to post replies

Write your reply to:

Draft