Search code examples
c#razorumbraco

Loop through options of a radio button from umbraco


I'm using a radio button list in umbraco cms:

enter image description here

I know how to get the value that the cms user chose, but how can I loop through (list out) all the options that the radio button list has?

Ideally I would like to have html like this which highlights the one they chose:

<ul>
<li>A</li>
<li class="chosen">B</li>
<li>C</li>
</ul>

I'd like to do this using Razor.


Solution

  • Get the id of the radio button list datatype you created then use DataTypeService to enumerate all options from that radio button list.

    <ul>
        @{
            var optionsDataTypeId = 1068; // your datatype id
            var selectedOption = Umbraco.GetPreValueAsString(Model.Content.GetPropertyValue<int>("favoritePet"));
            foreach (var option in Umbraco.DataTypeService.GetPreValuesCollectionByDataTypeId(optionsDataTypeId).PreValuesAsDictionary.Values)
            {
                <li class="@(option.Value == selectedOption ? "chosen" : "")">@option.Value</li>
            }
        }
    </ul>