Create NestedContent Items In SurfaceController

空扰寡人 提交于 2020-01-06 03:29:23

问题


I have two document types:

  1. FormSubmission
  2. FormField

The Form document type has a property named Fields which is a Nested Content data type that contains a list of FormField document types. I am trying to programmatically (in a SurfaceController) create a FormField and add it to the Fields property of the Form document type.

Here is the code I am trying to use to do this:

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);

On the newFormSubmission.SetValue("fields", newFormFields); line it throws this exception:

The best overloaded method match for 'Umbraco.Core.Models.ContentBase.SetPropertyValue(string, string)' has some invalid arguments

Anyone have any ideas how to store a list of DocumentTypes in the Nested Content data type?

PS: I am using Umbraco version 7.4.0 assembly: 1.0.5885.31226

UPDATE:

Lee Kelleher pointed me in the right direction towards developing my own solution in this post on the umbraco forms. I hope to have time after this project to polish up my solution and submit a pull request to the project.

I basically ended up creating some extension methods that take an IEnumerable<IContent> and return a JSON representation of the objects for the NestedContent plugin.


回答1:


This gist might help you:

robertjf/NestedContentCreator.cs

There's an example in the second file further down. It was written last year and may require some tweaking; but it should give you a good start.




回答2:


It seems that the second argument of the SetPropertyValue method expects a string and you are passing a List<Umbraco.Core.Models.IContent>



来源:https://stackoverflow.com/questions/36502706/create-nestedcontent-items-in-surfacecontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!