首页 > 解决方案 > 将自定义类属性映射到选择工厂选择项类

问题描述

我正在尝试在 Episerver 中实现 selectionFactory。选择工厂中的代码实现对于一些自定义类是通用的。

这是自定义类的 1

Public class Location ()
{
public virtual string City { get; set; }
public virtual string Name { get; set; }
public virtual string State { get; set; }
public virtual string Country { get; set; }
}

由于 selectionfactory 的返回类型是 SelectItem,我需要先将类转换为选择项类,这很容易通过类型检查和分配来实现。但是,由于这是多个类的通用代码,我需要继续添加条件。

 var locations = parentList as IEnumerable<Location>;
                    var selectItems = locations == null ? 
                        ((IEnumerable<string>)parentList).Select(item => new SelectItem() { Value = item, Text = item.ToString() }) :
                        locations.Select(location => new SelectItem() { Value = location.Name, Text = location.Name });

为了消除这种情况,我包含了一个自定义属性 (SelectionFactoryParentProperty),其中提到了下面给出的类型

[Display(Name = "Locations", GroupName = SystemTabNames.Content, Order = 70)]   
        [SelectionFactoryParentProperty(PropertyName = "Locations",PropertyType =typeof(Location))]
        [SelectOne(SelectionFactoryType = typeof(ParentListSelectionFactory))]
        public virtual string Locations { get; set; }

当两者之间没有共同属性时,有没有办法实现这一点,将本例中的类(位置)转换为 SelectItem?

任何输入表示赞赏。

问候。

标签: episerver

解决方案


推荐阅读