Select single category

Vote:
 

Hi,

This must be easy but I can't find an example that I can get to work.

I have a page property that is populated by a category list and this works fine by default allowing the user to select one or more categories from the list. I need to add another property which is also populated by a category list however this one should be single select only.

I have found the SelectOne attribute which looks like it should work however this prevents the category list being loaded.

Any thoughts or suggestions appreciated as always.

Thanks,

Mark

#117384
Feb 19, 2015 16:18
Vote:
 

Hi, Mark,

It's quite easy to create a CategorySelectionFactory. I don't think there is a built-in one, since there is PropertyCategoryList instead.

Here is a simple scenario:

[SelectionFactoryRegistration]
    public class CategorySelectionFactory : ISelectionFactory
    {
        public IEnumerable<ISelectItem> GetSelections(ExtendedMetadata metadata)
        {
            var categories = CategoryList.LoadCategories();
            var selectItems = new List<ISelectItem>();

            if (categories != null)
            {
                foreach (var cat in categories)
                {
                    var category = Category.Find(cat);
                    var catSelectItem = new SelectItem()
                    {
                        Text = category.LocalizedDescription,
                        Value = category.ID
                    };

                    selectItems.Add(catSelectItem);
                }

            }
            return selectItems;
        }
    }

Then, define the property:

        [SelectOne(SelectionFactoryType = typeof(CategorySelectionFactory))]
        public virtual int SelectOneCategory { get; set; }

It will return you the category ID, which you can use to find the category later:

var category = Category.Find(cat);

BR,

Marija


                        
#117416
Edited, Feb 20, 2015 11:07
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.