0

我正在尝试在 Orchard 中构建一项服务,该服务允许我通过页面上的自定义表单创建内容。服务和内容类型定义对我来说看起来不错,但不知何故,尽管我在 Orchard 日志文件中没有收到任何错误或其他迹象,但使用 IContentManager 创建新内容对我没有任何帮助。

涉及的零件

接受表单值的控制器

[HttpPost]
public ActionResult Create(CreateSopViewModel viewModel)
{
    if(!ModelState.IsValid)
    {
        var shape = _shape.CreateContent();
        shape.Header = _shape.Parts_Title(Title: "New item");

        // Add the original fields to the shape.
        shape.Title = viewModel.Title;
        shape.Description = viewModel.Description;
        shape.InitialComments = viewModel.InitialComments;

        return new ShapeResult(this, shape);
    }

    // Store the new procedure in the database
    _service.CreateContentItem(
        viewModel.Title,viewModel.Description,viewModel.InitialComments);

    // Redirect the user back to the homepage.
    return Redirect("~/");
}

包含 CreateContentItem 方法的服务:

public void CreateContentItem(string title, string description, string initialComments)
{
    // Initialize a new content item based on the SOP type
    var customPart = _services.ContentManager.New<MyCustomPart>("CustomContentType");

    customPart.Description = description;
    customPart.Identifier = BuildIdentifier(title);
    customPart.ContentItem.As<TitlePart>().Title = title;

    _services.ContentManager.Create(customPart.ContentItem);
}

内容部分+记录

public class MyCustomPart: ContentPart<MyCustomPartRecord>
{
    [Required]
    public string Identifier
    {
        get { return Record.Identifier; }
        set { Record.Identifier = value; }
    }

    [Required]
    public string Description
    {
        get { return Record.Description; }
        set { Record.Description = value; }
    }
}

public class MyCustomPartRecord: ContentPartRecord
{
    public virtual string Identifier { get; set; }

    public virtual string Description { get; set; }
}

迁移

SchemaBuilder.CreateTable(typeof(MyCustomPartRecord).Name, table => table
    .ContentPartRecord()
    .Column<string>("Description")
    .Column<string>("Identifier"));

ContentDefinitionManager.AlterPartDefinition("StandardOperationalProcedurePart", builder => builder
        .Attachable(true));

ContentDefinitionManager.AlterTypeDefinition("CustomContentType", builder => builder
    .DisplayedAs("Custom Content Type")
    .WithPart("TitlePart")
    .WithPart("MyCustomPart")
    .Creatable(true));

问题

同样,我没有收到任何错误,不是在日志中,也不是在 Visual Studio 中。但是,我的新内容项没有被创建,或者至少,我在内容下的站点管理部分中看不到它。

发生了什么,我该如何调试这种行为?

4

3 回答 3

2

我有一个类似的问题,当我使用采用枚举值的重载Create方法时解决了这个问题:VersionOptions

content.Create(customPart.ContentItem, VersionOptions.Published);

即使内容项不可创建,这也应该有效,因为我的不是。

于 2012-07-08T16:27:03.457 回答
1

我有一个类似的问题。就我而言,该项目最终确实出现了,但不是马上出现。

我的解决方案是:

_contentManager.Flush();
于 2013-08-29T11:09:51.617 回答
0

我遇到了这个问题,在我的情况下,我实际上在数据库中有一个错误(试图将 100 多个字符放入一个只能容纳 100 个字符的字段中!)。

我发现我得到的错误(Orchard.Indexing.Models.IndexingTaskRecord 条目中的空 id(发生异常后不要刷新会话)),实际上掩盖了这个问题。我不得不在日志中寻找真正的问题。

所以无论如何,我的建议是,如果您看到 contentmanager.create 似乎什么也没做,并且任何错误似乎都没有帮助,请仔细检查日志。它们可以在 Orchard.Web 主项目的 appdata 文件夹的日志子文件夹中找到。因为正如我在过去 48 小时内发现的那样,答案通常就在那里。

于 2016-01-08T14:43:55.223 回答