Copied to clipboard

Flag this post as spam?

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


  • RM 33 posts 95 karma points
    Aug 26, 2019 @ 20:10
    RM
    0

    Set RadioButtonList selected value programatially

    Hi all,

    I am having some issues with setting a value for a radiobuttonlist. I have been able to set the values for nested content, checkbox lists etc but this one has me stumped and the documentation doesn't seem to exist, even when things have broken from 7 to 8.

    Does anyone know what i have to pass to setValue to get it to populate the radio buton list selected value? I have sent the value, the id, the value and id, the value as an array etc, etc. and it still sits there unset.

    Any help would be greatly appreciated.

  • Shaishav Karnani from digitallymedia.com 354 posts 1638 karma points
    Aug 27, 2019 @ 03:14
    Shaishav Karnani from digitallymedia.com
    100

    Hi Roberto,

    I am on version 8.1 and created a Radio Button List datatype with 2 options ["Grid List" and "Table List"]. I tried to save with the name and it worked fine.

            var obj = _contentService.GetById(1074);
            obj.SetValue("eventType", "Grid List");
            _contentService.SaveAndPublish(obj);
    

    Also I have checked in the table Select * from umbracoPropertyData table that it stores as text value so no id is required anymore.

    Hope this helps.

    Regards,

    Shaishav

  • RM 33 posts 95 karma points
    Aug 28, 2019 @ 08:02
    RM
    0

    Hi Shaishav,

    Thanks for input, gave me the direction i needed to fix my solution. For anyone insterested, here is how i am getting the preValues from a list of selected item(s), needs to be made more general but hopefully gives people an idea.

    protected string GetSelectedPreValue(string dataTypeId, string selectedItem)
            {
                var selectedValue = GetDataTypeSelectedPreValues(dataTypeId, new List<string> { selectedItem })
                    .Select(x => x.Value)
                    .FirstOrDefault();
    
                return selectedValue;
            }
    protected string GetSelectedPreValues(string dataTypeId, List<string> selectedItems)
    {
        var selectedValues = GetDataTypeSelectedPreValues(dataTypeId, selectedItems)
            .Select(x => $"'{x.Value}'");
    
        return $"[{string.Join(",", selectedValues)}]";
    }
    
    protected List<ValueListItem> GetDataTypeSelectedPreValues(string dataTypeId, List<string> data)
    {
        var typeId = Guid.Parse(dataTypeId);
        var documentType = _dataTypeService.GetDataType(typeId);
    
        return documentType.GetSelectedPreValues(data).ToList();
    }
    

    `

Please Sign in or register to post replies

Write your reply to:

Draft