Copied to clipboard

Flag this post as spam?

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


  • Ruslan 6 posts 99 karma points
    Apr 18, 2016 @ 20:58
    Ruslan
    1

    Set values in CheckBox List code behind

    Hi all, I have such code that created new node in Umbraco.

     IContent story = contentService.CreateContent(model.StoryTitle, folderId, "successStory", 0);
     story.SetValue("storyStates", model.States);
    

    How I can set value in CheckBox List property?

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Apr 19, 2016 @ 19:00
    Dan Diplo
    102

    As far as I remember a checkbox list contains a comma-separated list of Ids. So you can set it like:

    story.SetValue("checkboxList", "1,2,3,4,5");
    

    or

    var values = new int[] { 1, 2, 3, 4, 5 };
    story.SetValue("checkboxList", String.Join(",", values));
    
  • Ruslan 6 posts 99 karma points
    Apr 19, 2016 @ 21:28
    Ruslan
    1

    Dan Diplo Thank you very much! It's solved my problem.

  • Liam Laverty 8 posts 100 karma points c-trib
    Feb 15, 2024 @ 11:00
    Liam Laverty
    0

    This has changed in Umbraco V13. The Checkbox List datatype now contains string representation of an array, containing a CSV.

    So where @Dan Diplo's

    var values = new int[] { 1, 2, 3, 4, 5 };story.SetValue("checkboxList", 
    String.Join(",", values));
    

    would create the value 1, 2, 3, 4, 5, you'd now need to create a string which contains the square brackets, like [1, 2, 3, 4, 5]

    Updated code:

    var values = new int[] { 1, 2, 3, 4, 5 };story.SetValue("checkboxList", 
    $"[{String.Join(",", values )}]");
    
Please Sign in or register to post replies

Write your reply to:

Draft