Copied to clipboard

Flag this post as spam?

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


  • macleodmorgan 2 posts 22 karma points
    Aug 02, 2023 @ 14:01
    macleodmorgan
    0

    Loop through and display checbox items

    I am trying to loop through and display ALL the items in my checkbox list but it only displays the ones that I have checked.

    @if (rowItem.AgeGroupSelector.Any())
                    {
    
                        <ul>
                            @foreach (var ageItem in rowItem.AgeGroupSelector)
                            {
                                <li><input type="checkbox" id="age" name="age">
                                <label for="age">@ageItem</label><br></li>
                            }
                        </ul>
                    }
    

    It is the checkbox list data type that I am using. Lets say I have values 1, 2, 3, 4, 5 available to be selected in the backend. If I select values 2 and 3 then my loop will only print these two values. I would like it to print 1, 2, 3, 4, 5 even if they haven't been selected.

  • Huw Reddick 1929 posts 6697 karma points MVP 2x c-trib
    Aug 02, 2023 @ 15:19
    Huw Reddick
    0

    Hi macleodmorgan,

    That is correct since your property value only contains a reference to the items selected. It is technically possible to get the whole list, but you will need to inject the IDataTypeService into your view, you can then do something like this

    @{
        var cblDataType = dataTypeService.GetDataType("Home - Checkbox list");
        var config = (Umbraco.Cms.Core.PropertyEditors.ValueListConfiguration)cblDataType.Configuration;
        foreach (var item in config.Items)
        {
            if (Model.CheckBoxTest.Contains(item.Value))
            {
                <input type="checkbox" value="@item.Value" checked /> @item.Value
            }
            else
            {
                <input type="checkbox" value="@item.Value" /> @item.Value
            }
    
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft