创建自定义 ContentPart Orchard CMS 时出错
Error Creating Custom ContentPart Orchard CMS
进入管理仪表板中内容的编辑器页面时,我收到错误 "The model item passed into the dictionary is of type 'Orchard.UI.Zones.ZoneHolding', but this dictionary requires a model item of type'First.Module.Models.PersonListPart'"。
我的内容驱动看起来像:
public class PersonListPartDriver : ContentPartDriver<PersonListPart>
{
protected override DriverResult Display(
PersonListPart part, string displayType, dynamic shapeHelper) {
return ContentShape("Parts_PersonList",
() => shapeHelper.Parts_PersonList(
MaxCount: part.MaxCount
));
}
protected override DriverResult Editor(
PersonListPart part, dynamic shapeHelper) {
return ContentShape("Parts_PersonList_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/PersonList",
Models: part,
Prefix: Prefix
));
}
//POST
protected override DriverResult Editor(
PersonListPart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
}
我的 PersonListPart 模型如下所示:
namespace First.Module.Models {
public class PersonListPart : ContentPart<PersonListPartRecord> {
public int MaxCount {
get { return Record.MaxCount; }
set { Record.MaxCount = value; }
}
}
public class PersonListPartRecord : ContentPartRecord {
public virtual int MaxCount { get; set; }
}
}
它加载内容部分的编辑器 CSHTML,如下所示:
@model First.Module.Models.PersonListPart
<fieldset>
<legend>@T("Person List Settings")</legend>
<ol>
<li>
@Html.LabelFor(m => m.MaxCount, T("Maximal count"));
@Html.TextBoxFor(m => m.MaxCount, new { @class = "text small" })
</li>
</ol>
</fieldset>
@model 有问题。
在调试和注释掉@model 时,Model.ContentItem.Part[0] 有项目,但作为 Orchard CMS 的新手,我不知道如何正确使用它。
PersonListPartDriver Editor 方法中有错字。由于 EditorTemplate() 中的值是动态的,因此很容易产生拼写错误,在本例中,它产生了一个难以调试的错误。
protected override DriverResult Editor(
PersonListPart part, dynamic shapeHelper) {
return ContentShape("Parts_PersonList_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/PersonList",
Models: part,
Prefix: Prefix
));
}
修复
Models: part,
应该是
Model: part,
进入管理仪表板中内容的编辑器页面时,我收到错误 "The model item passed into the dictionary is of type 'Orchard.UI.Zones.ZoneHolding', but this dictionary requires a model item of type'First.Module.Models.PersonListPart'"。
我的内容驱动看起来像:
public class PersonListPartDriver : ContentPartDriver<PersonListPart>
{
protected override DriverResult Display(
PersonListPart part, string displayType, dynamic shapeHelper) {
return ContentShape("Parts_PersonList",
() => shapeHelper.Parts_PersonList(
MaxCount: part.MaxCount
));
}
protected override DriverResult Editor(
PersonListPart part, dynamic shapeHelper) {
return ContentShape("Parts_PersonList_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/PersonList",
Models: part,
Prefix: Prefix
));
}
//POST
protected override DriverResult Editor(
PersonListPart part, IUpdateModel updater, dynamic shapeHelper)
{
updater.TryUpdateModel(part, Prefix, null, null);
return Editor(part, shapeHelper);
}
}
我的 PersonListPart 模型如下所示:
namespace First.Module.Models {
public class PersonListPart : ContentPart<PersonListPartRecord> {
public int MaxCount {
get { return Record.MaxCount; }
set { Record.MaxCount = value; }
}
}
public class PersonListPartRecord : ContentPartRecord {
public virtual int MaxCount { get; set; }
}
}
它加载内容部分的编辑器 CSHTML,如下所示:
@model First.Module.Models.PersonListPart
<fieldset>
<legend>@T("Person List Settings")</legend>
<ol>
<li>
@Html.LabelFor(m => m.MaxCount, T("Maximal count"));
@Html.TextBoxFor(m => m.MaxCount, new { @class = "text small" })
</li>
</ol>
</fieldset>
@model 有问题。
在调试和注释掉@model 时,Model.ContentItem.Part[0] 有项目,但作为 Orchard CMS 的新手,我不知道如何正确使用它。
PersonListPartDriver Editor 方法中有错字。由于 EditorTemplate() 中的值是动态的,因此很容易产生拼写错误,在本例中,它产生了一个难以调试的错误。
protected override DriverResult Editor(
PersonListPart part, dynamic shapeHelper) {
return ContentShape("Parts_PersonList_Edit",
() => shapeHelper.EditorTemplate(
TemplateName: "Parts/PersonList",
Models: part,
Prefix: Prefix
));
}
修复
Models: part,
应该是
Model: part,