【问题标题】:Create NestedContent Items In SurfaceController在 SurfaceController 中创建 NestedContent 项
【发布时间】:2016-04-08 14:50:53
【问题描述】:

我有两种文档类型:

  1. 表单提交
  2. 表单域

Form 文档类型有一个名为 Fields 的属性,它是一个包含 FormField 列表的 Nested Content 数据类型 文档类型。我正在尝试以编程方式(在 SurfaceController 中)创建一个 FormField 并将其添加到 Form 文档类型的 Fields 属性中。

这是我尝试使用的代码:

var newFormFields = new List<Umbraco.Core.Models.IContent>();
int i = 0;
foreach (var formField in model.Fields)
{
    string fieldName = string.Format("Field {0}", i);
    var newFormField = contentService.CreateContent(fieldName, newFormSubmission.Id, "formFieldSubmission", formNode.CreatorId);
    newFormField.SetValue("fieldName", formField.Name);
    newFormField.SetValue("fieldType", formField.Type);
    newFormField.SetValue("manditory", formField.Manditory);
    newFormField.SetValue("fieldValue", formField.Value);
    newFormFields.Add(newFormField);
    ++i;
}

newFormSubmission.SetValue("fields", newFormFields);

var status = contentService.SaveAndPublishWithStatus(newFormSubmission, formNode.CreatorId, raiseEvents: false);

newFormSubmission.SetValue("fields", newFormFields); 行它会抛出这个异常:

'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)'的最佳重载方法匹配有一些无效参数

有人知道如何在 Nested Content 数据类型中存储 DocumentTypes 列表吗?

PS:我正在使用 Umbraco 版本 7.4.0 程序集:1.0.5885.31226

更新:

Lee Kelleher 为我指明了在this post on the umbraco forms 中开发自己的解决方案的正确方向。我希望在这个项目之后有时间完善我的解决方案并向项目提交拉取请求。

我基本上最终创建了一些扩展方法,它们采用 IEnumerable&lt;IContent&gt; 并返回 NestedContent plugin 的对象的 JSON 表示。

【问题讨论】:

  • 有点 OT,但您是否偶然尝试重新创建 Umbraco Forms 样式功能?这里有一个开源替代方案:github.com/rhythmagency/formulate
  • @JannikAnker 我希望它是那么容易。在这个项目中,我们有更具体的要求,不幸的是 Umbraco Forms 不支持。我希望他们这样做,但因此我们无法使用它......我希望我们可以。
  • 以防万一它可能有用,Formulate 现已上线:formulate.rocks

标签: c# umbraco umbraco7


【解决方案1】:

这个要点可能会对你有所帮助:

robertjf/NestedContentCreator.cs

下面的第二个文件中有一个示例。它是去年写的,可能需要一些调整;但它应该会给你一个好的开始。

【讨论】:

  • 这确实帮助了我很多@robert-foster。谢谢!
  • 很高兴为您服务:)
【解决方案2】:

似乎SetPropertyValue 方法的第二个参数需要string,而您传递的是List&lt;Umbraco.Core.Models.IContent&gt;

【讨论】:

  • 我同意这似乎是问题所在;但是,当我查看 SetValue 方法的方法签名时,它是(字符串、对象)。显然,该方法正在调用另一个具有签名(字符串,字符串)的方法。我怀疑我需要将 NestedContent 数据序列化为 JSON 字符串,但希望有人以前遇到过这个问题并有更好的提示。
  • 您可以查看 Umbraco 源代码(在 Github 上)以确认您的怀疑?
  • 我会尝试减少字段,看看其中哪一个失败了。目前只添加一个字段:newFormField.SetValue("fieldName", formField.Name); 我们需要知道哪个属性失败(名称?类型?Manditory?值?)
  • SetPropertyValue 函数调用SetValueOnProperty(propertyTypeAlias, value); 函数。正如@JannikAnker 指出的那样,您可以在此处查看代码:github.com/umbraco/Umbraco-CMS/blob/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多