Copied to clipboard

Flag this post as spam?

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


  • Dan Sørensen 102 posts 327 karma points
    Mar 23, 2015 @ 16:44
    Dan Sørensen
    0

    get all items from Umbraco.CheckBoxList

    I hope this is an easy question.

    If I get items from the Umbraco.CheckBoxList like this

     string values = currentPage.GetPropertyValue("proppertyName").ToString();
    

    I only get the ones selected in an string like this Item1, Item2, Item3 what if I need them all to be displayed on the frontend and only the one selected in bold how would I select them all.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Mar 23, 2015 @ 17:04
    Jeavon Leopold
    100

    Hi Dan,

    I like this question a lot, however the answer is a bit complex and requires using a few services!

    @{
        if (Model.Content.HasValue("superHeros"))
        {
            var selectedValues = Model.Content.GetPropertyValue<string>("superHeros").Split(',');
    
            var propertyTypeAlias = Model.Content.GetProperty("superHeros").PropertyTypeAlias;
            var documentTypeAlias = Model.Content.DocumentTypeAlias;
    
            var propertyType = ApplicationContext.Services.ContentTypeService.GetContentType(documentTypeAlias).PropertyTypes.FirstOrDefault(x => x.Alias == propertyTypeAlias);
    
            if (propertyType != null)
            {
                var dataTypeId = propertyType.DataTypeDefinitionId;
                var preValues = ApplicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(dataTypeId);
    
                foreach (var preValue in preValues)
                {
                    if (selectedValues.Contains(preValue))
                    {
                        <li><strong>@preValue</strong></li>
                    }
                    else
                    {
                        <li>@preValue</li>
                    }
                }
            }
        }
    }
    

    Because this requires quite a few management API's to get the answer, I would recommend using a cached partial view so that it's not hitting the DB a lot.

    Jeavon

  • John Churchley 272 posts 1258 karma points c-trib
    Mar 23, 2015 @ 17:07
    John Churchley
    1

    @foreach (var n in ApplicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(datatypeid).ToList())

    {

        @n

    }

    datatypeid circled
     
  • Dan Sørensen 102 posts 327 karma points
    Apr 08, 2015 @ 09:40
    Dan Sørensen
    1

    Sorry for the late response Jeavon Leopold and John Churchley I have been on a vacation for some days :D.

    I appreciate the answers !

    I tried your solution Jeavon and it worked great thanks !

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Apr 08, 2015 @ 11:38
    Jeavon Leopold
    0

    Cool, looking back at it now I might put the propertyAlias into a variable to avoid it being there three times, e.g.

    @{
        var propertyAlias = "superHeros";
        if (Model.Content.HasValue(propertyAlias))
        {
            var selectedValues = Model.Content.GetPropertyValue<string>(propertyAlias).Split(',');
    
            var propertyTypeAlias = Model.Content.GetProperty(propertyAlias).PropertyTypeAlias;
            var documentTypeAlias = Model.Content.DocumentTypeAlias;
    
            var propertyType = ApplicationContext.Services.ContentTypeService.GetContentType(documentTypeAlias).PropertyTypes.FirstOrDefault(x => x.Alias == propertyTypeAlias);
    
            if (propertyType != null)
            {
                var dataTypeId = propertyType.DataTypeDefinitionId;
                var preValues = ApplicationContext.Services.DataTypeService.GetPreValuesByDataTypeId(dataTypeId);
    
                foreach (var preValue in preValues)
                {
                    if (selectedValues.Contains(preValue))
                    {
                        <li><strong>@preValue</strong></li>
                    }
                    else
                    {
                        <li>@preValue</li>
                    }
                }
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft