我创建了一个自定义类型Project,它有4个图像,我希望使用4个类型的媒体选择器,我添加了链接...
public int Create()
{
// Define the project type
ContentDefinitionManager.AlterTypeDefinition("Project", cfg => cfg
.WithSetting("Stereotype", "Content")
.CommomPart()
.AutoroutePart("our-work")
.BodyPart()
.WithPart("TitlePart")
.WithPart("PublishLaterPart")
.WithPart("MenuPart", builder => builder
.WithSetting("MenuPart.OnMenu", "true")
.WithSetting("MenuPart.CurrentMenu", "Project Menu"))
.WithPart("Project")
.Creatable()
.Draftable());
return 1;
}
public int UpdateFrom1()
{
// Define project part - having a part with the same name will create fields in the project type
ContentDefinitionManager.AlterPartDefinition("Project", builder => builder
.MediaPickerField("MainImage")
.MediaPickerField("MediumImage")
.MediaPickerField("SmallImage")
.MediaPickerField("Logo")
.TextField("ShortDescription", Flavour.TextArea)
.TextField("Features", Flavour.Markdown)
.TextField("ClientTitle", Flavour.DefaultFlavour)
.TextField("ClientName", Flavour.DefaultFlavour)
.TextField("ClientQuote", Flavour.Textarea)
.BooleanField("MainProjectOnHomePage", false)
.Attachable());
return 2;
}
扩展方法用于使其更简洁
public static ContentPartDefinitionBuilder MediaPickerField(this ContentPartDefinitionBuilder builder,
string name, bool required = true, string hint = "")
{
var displayName = SplitCamel(name);
// default implementation of Media picker field - create overloads for more options
return builder.WithField(name, fieldBuilder => fieldBuilder
.OfType("MediaPickerField")
.WithDisplayName(displayName)
.WithSetting("MediaPickerFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
.WithSetting("MediaPickerFieldSettings.AllowedExtensions", "jpg png gif")
.WithSetting("MediaPickerFieldSettings.Hint", hint));
}
public static ContentPartDefinitionBuilder TextField(this ContentPartDefinitionBuilder builder,
string name, Flavour flavor, bool required = true, string hint = "")
{
var strFlavor = SplitCamel(flavor.ToString());
// default implementation of Media picker field - create overloads for more options
return builder.WithField(name, fieldBuilder => fieldBuilder
.OfType("TextField")
.WithSetting("TextFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
.WithSetting("TextFieldSettings.Flavor", strFlavor)
.WithSetting("TextFieldSettings.Hint", hint));
}
我能够将媒体添加到仪表板中的4个字段,但是在保存时只保存了第一个媒体项目,我可以保存项目,即使认为所有媒体文件都是必需的,但是3个媒体项目字段丢失,
我还尝试通过仪表板添加额外的媒体选择器,结果类似。
我做错了吗?