Quantcast
Channel: Optimizely Search & Navigation
Viewing all articles
Browse latest Browse all 6894

Custom selection factory populated with values from category repository

$
0
0

We've created a custom selection factory for retrieving parent categories (i.e. categories with children, regardless of what their own parent category are) from the category repository. The list is rendered - ok (first image) The value is set - ok (second image - we can also tell the value is set from debugging and because the values are rendered in the view). But, when the block that has this selection factory is published, the selected value in the dropdown list appears empty (third image), although the value is set. Anyone experienced something similar and know to solve it?

The selection factory:

    public class MainCategorySelectionFactory : ISelectionFactory
    {
        private Injected<CategoryRepository> CategoryRepository;
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            var categories = CategoryRepository.Service.GetRoot().Categories;
            var str = string.Empty;
            var list = new List<Category>();
            var strings = new List<string>();
            var selectableCategories = GetCategoriesWithChildren(categories,str, strings, list)
                .Select((x, i) => new SelectItem
                {
                    Text = strings[i],
                    Value = x.ID
                });
            return selectableCategories;
        }
        private string GetCategoryDescriptionAndAncestors(Category cat, string str)
        {
            return cat.Parent.Name != Category.RootName
                ? str + " - " + cat.Description
                : cat.Description;
        }
        private List<Category> GetCategoriesWithChildren(CategoryCollection categories, string str, List<string> strings, List<Category> list)
        {
            foreach (var cat in categories.Where(x => x.Categories.Any()))
            {
                // Add current category and its ancestor path to lists
                list.Add(cat);
                strings.Add(GetCategoryDescriptionAndAncestors(cat, str));
                GetCategoriesWithChildren(
                    cat.Categories, 
                    GetCategoryDescriptionAndAncestors(cat, str), 
                    strings, 
                    list);
            }
            return list;
        }
    }

The model:

        [Display(
            GroupName = GroupNames.Content,
            Order = 10)]
        [SelectOne(SelectionFactoryType = typeof(MainCategorySelectionFactory))]
        public virtual string MainCategory { get; set; }

What am I missing?


Viewing all articles
Browse latest Browse all 6894

Trending Articles