我创建了一个新的内容部分。我将内容部分附加到页面内容项。编辑“页面内容”项时,自定义内容部件编辑器(我创建的)显示正确,仅包含两个字段。
当我填写数据并点击“保存”时,驱动程序的POST编辑器操作正在接收自定义部件的空值。我在Part类中使用InfoSet。但是,我还创建了一个迁移来为部件记录创建表。
以下是一些代码:
using Orchard.ContentManagement;
using Orchard.ContentManagement.Records;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace SlideShare.Models
{
public class SlideSharePartRecord : ContentPartRecord
{
public virtual string SlideShareId { get; set; }
public virtual int StartFromSlide { get; set; }
}
public class SlideSharePart : ContentPart<SlideSharePartRecord>
{
[Required]
public string SlideShareId
{
get { return Retrieve(x => x.SlideShareId); }
set { Store(x => x.SlideShareId, value); }
}
[Required]
[Range(1,20)]
public int StartFromSlide
{
get { return Retrieve(x => x.StartFromSlide); }
set { Store(x => x.StartFromSlide, value); }
}
}
}
using Orchard.ContentManagement.Handlers;
using Orchard.Data;
using System;
using System.Collections.Generic;
using SlideShare.Models;
namespace SlideShare.Handlers
{
public class SlideShareHandler : ContentHandler
{
public SlideShareHandler(IRepository<SlideSharePartRecord> repository)
{
Filters.Add(StorageFilter.For(repository));
}
}
}
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using SlideShare.Models;
using System;
using System.Collections.Generic;
namespace SlideShare.Drivers
{
public class SlideShareDriver : ContentPartDriver<SlideSharePart>
{
protected override string Prefix { get { return "SlideShare"; } }
protected override DriverResult Display(SlideSharePart part, string displayType, dynamic shapeHelper)
{
return ContentShape("Parts_SlideShare", () => shapeHelper.Parts_SlideShare(
SlideShareId: part.SlideShareId,
StartsFromSlide: part.StartFromSlide));
}
// GET
protected override DriverResult Editor(SlideSharePart part, dynamic shapeHelper)
{
return ContentShape("Parts_SlideShare_Edit", () => shapeHelper.EditorTemplate(
TemplateName: "Parts.SlideShare",
Model: part,
Prfix: Prefix));
}
// POST
protected override DriverResult Editor(SlideSharePart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
}
}
@using System.Web.Mvc.Html
@model SlideShare.Models.SlideSharePart
<fieldset>
<legend>@T("SlideShare Presentation Fields")</legend>
<div class="editor-field">
@Html.LabelFor(x => x.SlideShareId, T("SlideShare Id"))
@Html.EditorFor(x => x.SlideShareId)
@Html.ValidationMessageFor(x => x.SlideShareId)
</div>
<div class="hint">@T("Enter the SlideShare Presentation Id")</div>
<div class="editor-field">
@Html.LabelFor(x => x.StartFromSlide, T("Start From Slide"))
@Html.EditorFor(x => x.StartFromSlide)
@Html.ValidationMessageFor(x => x.StartFromSlide)
</div>
<div class="hint">@T("Enter the Start From Slide")</div>
</fieldset>