Copied to clipboard

Flag this post as spam?

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


  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 10, 2017 @ 23:19
    Nicholas Westby
    0

    Ditto Processor for Drop Down Multiple?

    I've got a drop down multiple that allows content editors to select multiple days of the week (e.g., "Monday" and "Tuesday"). If I do this:

    [UmbracoProperty]
    public IEnumerable<string> Days { get; set; }
    

    The value doesn't seem to get set because the result of [UmbracoProperty] is a string (e.g., "Monday,Tuesday"). Is there a built in processor to return a collection of strings for a drop down multiple?

  • Nicholas Westby 2054 posts 7103 karma points c-trib
    Aug 10, 2017 @ 23:39
    Nicholas Westby
    0

    I think I know the answer to this: https://our.umbraco.org/documentation/getting-started/backoffice/property-editors/built-in-property-editors/dropdown-list-multiple

    It appears that Umbraco's default behavior is to return a single string. Suppose I'll just create a processor to split the string based on commas. Not ideal, but what're you gonna do.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Aug 11, 2017 @ 06:47
    Lee Kelleher
    100

    Yeah, we had to roll our own processor for this.

    using System;
    using System.Linq;
    using Umbraco.Core;
    
    namespace Our.Umbraco.Ditto
    {
        public class DelimitedListAttribute : DittoProcessorAttribute
        {
            protected string _delimiter;
    
            public DelimitedListAttribute(string delimiter = ",")
            {
                _delimiter = delimiter;
            }
    
            public override object ProcessValue()
            {
                var strValue = Value == null
                    ? string.Empty
                    : Value.ToString();
    
                return string.IsNullOrWhiteSpace(strValue)
                    ? Enumerable.Empty<string>()
                    : strValue.Split(new[] { _delimiter }, StringSplitOptions.RemoveEmptyEntries)
                        .Select(x => x.Trim())
                        .Where(x => !string.IsNullOrWhiteSpace(x))
                        .ToList();
            }
        }
    }
    

    Funny thing is that we've only used it on one project so far.

    Cheers,
    - Lee

Please Sign in or register to post replies

Write your reply to:

Draft