使用果园 1.7。
目标:是创建我们称之为页面过滤器的集合。
要创建多对多部分,我按照此处描述的教程步骤进行操作
问题:我的驱动程序类中的编辑器覆盖永远不会被击中,并且正在返回一个只有保存按钮的空创建页面。
模型类:
namespace MyLib.Orchard.HyperlinkItemManager.Models
{
public class HyperlinkPageFilterRecord
{
public virtual int Id { get; set; }
public virtual int PageId { get; set; }
}
}
namespace MyLib.Orchard.HyperlinkItemManager.Models
{
public class HyperlinkPageFiltersContentRecord
{
public virtual int Id { get; set; }
public virtual HyperlinkPageFiltersPartRecord HyperlinkPageFiltersPartRecord { get; set; }
public virtual HyperlinkPageFilterRecord HyperlinkPageFilterRecord { get; set; }
}
}
namespace MyLib.Orchard.HyperlinkItemManager.Models
{
public class HyperlinkPageFiltersPart : ContentPart<HyperlinkPageFiltersPartRecord>
{
public IEnumerable<HyperlinkPageFilterRecord> HyperlinkPageFilters
{
get
{
return Record.HyperlinkPageFilters.Select(r => r.HyperlinkPageFilterRecord);
}
}
}
}
namespace MyLib.Orchard.HyperlinkItemManager.Models
{
public class HyperlinkPageFiltersPartRecord : ContentPartRecord
{
public virtual IList<HyperlinkPageFiltersContentRecord> HyperlinkPageFilters { get; set; }
public HyperlinkPageFiltersPartRecord()
{
HyperlinkPageFilters = new List<HyperlinkPageFiltersContentRecord>();
}
}
}
查看模型类:
namespace MyLib.Orchard.HyperlinkItemManager.Models
{
public class HyperlinkPageFiltersEditViewModel
{
public IList<HyperlinkPageFilterEntry> HyperlinkPageFilters { get; set; }
}
public class HyperlinkPageFilterEntry
{
public HyperlinkPageFilterRecord HyperlinkPageFilter { get; set; }
public bool IsChecked { get; set; }
}
}
服务:
namespace MyLib.Orchard.HyperlinkItemManager.Services
{
public interface IHyperlinkPageFiltersService : IDependency
{
void UpdateHyperlinkPageFiltersForContentItem(
ContentItem item,
IEnumerable<HyperlinkPageFilterEntry> HyperlinkPageFilters);
IEnumerable<HyperlinkPageFilterRecord> GetHyperlinkPageFilters();
}
public class HyperlinkPageFiltersService : IHyperlinkPageFiltersService
{
private readonly IRepository<HyperlinkPageFilterRecord>
_HyperlinkPageFilterRepository;
private readonly IRepository<HyperlinkPageFiltersContentRecord>
_contentHyperlinkPageFiltersRepository;
public HyperlinkPageFiltersService(
IRepository<HyperlinkPageFilterRecord> HyperlinkPageFilterRepository,
IRepository<HyperlinkPageFiltersContentRecord> contentHyperlinkPageFiltersRepository)
{
_HyperlinkPageFilterRepository = HyperlinkPageFilterRepository;
_contentHyperlinkPageFiltersRepository = contentHyperlinkPageFiltersRepository;
}
public void UpdateHyperlinkPageFiltersForContentItem(
ContentItem item,
IEnumerable<HyperlinkPageFilterEntry> HyperlinkPageFilters)
{
var record = item.As<HyperlinkPageFiltersPart>().Record;
var oldHyperlinkPageFilters = _contentHyperlinkPageFiltersRepository.Fetch(
r => r.HyperlinkPageFiltersPartRecord == record);
var lookupNew = HyperlinkPageFilters
.Where(e => e.IsChecked)
.Select(e => e.HyperlinkPageFilter)
.ToDictionary(r => r, r => false);
// Delete the rewards that are no longer there
// and mark the ones that should stay
foreach (var contentHyperlinkPageFiltersRecord in oldHyperlinkPageFilters)
{
if (lookupNew.ContainsKey(
contentHyperlinkPageFiltersRecord.HyperlinkPageFilterRecord))
{
lookupNew[contentHyperlinkPageFiltersRecord.HyperlinkPageFilterRecord]
= true;
}
else
{
_contentHyperlinkPageFiltersRepository.Delete(contentHyperlinkPageFiltersRecord);
}
}
// Add the new rewards
foreach (var HyperlinkPageFilter in lookupNew.Where(kvp => !kvp.Value)
.Select(kvp => kvp.Key))
{
_contentHyperlinkPageFiltersRepository.Create(new HyperlinkPageFiltersContentRecord
{
HyperlinkPageFiltersPartRecord = record,
HyperlinkPageFilterRecord = HyperlinkPageFilter
});
}
}
public IEnumerable<HyperlinkPageFilterRecord> GetHyperlinkPageFilters()
{
return _HyperlinkPageFilterRepository.Table.ToList();
}
移民:
public int UpdateFrom11()
{
SchemaBuilder.CreateTable("HyperlinkPageFiltersPartRecord",
table => table
.ContentPartRecord()
);
SchemaBuilder.CreateTable("HyperlinkPageFilterRecord",
table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<int>("PageId")
);
SchemaBuilder.CreateTable("HyperlinkPageFiltersContentRecord",
table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<int>("HyperlinkPageFiltersPartRecord_Id")
.Column<int>("HyperlinkPageFilterRecord_Id")
);
return 12;
}
public int UpdateFrom12() {
ContentDefinitionManager.AlterTypeDefinition("HyperlinkPageFiltersPart", builder => builder
.DisplayedAs("Hyperlink Page Filters")
.WithPart("HyperlinkPageFiltersPart")
);
return 13;
}
public int UpdateFrom13()
{
ContentDefinitionManager.AlterPartDefinition(
"HyperlinkPageFiltersPart",
builder => builder.Attachable());
return 14;
}
}
}
最后……我们没有撞到的司机……
namespace MyLib.Orchard.HyperlinkItemManager.Drivers
{
[UsedImplicitly]
public class HyperlinkPageFiltersPartDriver : ContentPartDriver<HyperlinkPageFiltersPart>
{
private readonly IHyperlinkPageFiltersService _hyperlinkPageFiltersService;
private IContentManager _contentManager;
//private const string TemplateName = "Parts/HyperlinkPageFilters";
public HyperlinkPageFiltersPartDriver(IHyperlinkPageFiltersService hyperlinkPageFiltersService, IContentManager contentManager)
{
_hyperlinkPageFiltersService = hyperlinkPageFiltersService;
_contentManager = contentManager;
}
protected override string Prefix
{
get { return "HyperlinkPageFilters"; }
}
protected override DriverResult Display(HyperlinkPageFiltersPart part,string displayType,dynamic shapeHelper)
{
return ContentShape("Parts_HyperlinkPageFilters", () => shapeHelper.Parts_HyperlinkPageFilters(
ContentPart: part,
HyperlinkPageFilters: part.HyperlinkPageFilters));
}
protected override DriverResult Editor(HyperlinkPageFiltersPart part, dynamic shapeHelper)
{
return ContentShape("Parts_HyperlinkPageFilters_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts.HyperlinkPageFilters.Edit",
Model: BuildEditorViewModel(part),
Prefix: Prefix));
}
protected override DriverResult Editor(HyperlinkPageFiltersPart part, IUpdateModel updater, dynamic shapeHelper)
{
var model = new HyperlinkPageFiltersEditViewModel();
updater.TryUpdateModel(model, Prefix, null, null);
if (part.ContentItem.Id != 0)
{
_hyperlinkPageFiltersService.UpdateHyperlinkPageFiltersForContentItem(
part.ContentItem, model.HyperlinkPageFilters);
}
return Editor(part, shapeHelper);
}
private HyperlinkPageFiltersEditViewModel BuildEditorViewModel(HyperlinkPageFiltersPart part)
{
//get all pages
var pages = _contentManager.Query("Pages");
//get pagesfilters
//check pages
var itemRewards = part.HyperlinkPageFilters.ToLookup(r => r.Id);
return new HyperlinkPageFiltersEditViewModel
{
HyperlinkPageFilters = _hyperlinkPageFiltersService.GetHyperlinkPageFilters().Select(
r => new HyperlinkPageFilterEntry
{
HyperlinkPageFilter = r,
IsChecked = itemRewards.Contains(r.Id)
}).ToList()
};
}
}
}
处理程序:
namespace MyLib.Orchard.HyperlinkItemManager.Handlers {
public class HyperlinkPageFiltersPartHandler : ContentHandler {
public HyperlinkPageFiltersPartHandler(IRepository<HyperlinkPageFiltersPartRecord> repository) {
Filters.Add(StorageFilter.For(repository));
}
}
}