0

我需要使用代码创建内容项。我知道有内置方式,但我希望在模块中有自己的功能。我尝试了代码,但它给了我错误。所有相关代码如下。内容部分名称为“Course”,内容项名称为“Courses”

“你调用的对象是空的。”

[HiddenInput(DisplayValue = false)]
public int Id {
get { return ContentItem.Id; }
}

**Controller**

  public ActionResult Create()
    {            
        var course = _orchardService.ContentManager.New("Courses");
        dynamic model = _orchardService.ContentManager.BuildEditor(course);

        return View((object)model);                                  
    }


    [HttpPost, ActionName("Create")]
    public ActionResult CreatePOST(string idl)
    {

        var contentItem = _contentManager.New("Courses");

        _contentManager.Create(contentItem, VersionOptions.Draft);

        dynamic model = _contentManager.UpdateEditor(contentItem, this);

        _contentManager.Publish(contentItem);

        _orchardService.Notifier.Information(new LocalizedString("Your content has been created."));

        var adminRouteValues = _contentManager.GetItemMetadata(contentItem).AdminRouteValues;

        return RedirectToRoute(adminRouteValues);
    }


    public ActionResult Index(PagerParameters pagerParameters, CourseSearchVM search)
    {
        //this is displaying only published content
        var courseQuery = _contentManager.Query<CoursePart>().List().ToList();
        // Project the query into a list of customer shapes
        var coursesProjection = from course in courseQuery
                                  select Shape.course
                                  (
                                    Id: course.Id,
                                    Name: course.Name,
                                    Description: course.Description
                                  );

        // The pager is used to apply paging on the query and to create a PagerShape
        var pager = new Pager(_siteService.GetSiteSettings(), pagerParameters.Page, pagerParameters.PageSize);
        // Apply paging
        var coures = coursesProjection.Skip(pager.GetStartIndex()).Take(pager.PageSize);
        // Construct a Pager shape
        var pagerShape = Shape.Pager(pager).TotalItemCount(courseQuery.Count());
        // Create the viewmodel
        var model = new CourseIndexVM(coures, search, pagerShape);
        return View(model);
    }


**CoursePart Model**

public class CoursePart : ContentPart<CoursePartRecord>
{
    public string Name
    {
        get { return Record.Name; }
        set { Record.Name = value; }
    }

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

public class CoursePartRecord : ContentPartRecord
{
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}


**Create.cshtml**

@{ Layout.Title = "Edit Course"; }

@using(Html.BeginFormAntiForgeryPost())
{
@Display(Model)
}

 **Course.cshtml**

 @model Course.Models.CoursePart
 <fieldset> 
<div class="editor-label">@Html.LabelFor(x => x.Name)</div>
<div class="editor-field">
    @Html.EditorFor(x => x.Name)
    @Html.ValidationMessageFor(x => x.Name)
</div>

<div class="editor-label">@Html.LabelFor(x => x.Description)</div>
<div class="editor-field">
    @Html.EditorFor(x => x.Description)
    @Html.ValidationMessageFor(x => x.Description)
</div>
</fieldset>
4

2 回答 2

0

您正在尝试将 ContentItem 转换为 ContentPart。那是行不通的,而不是在您的 createPost 操作中,您应该调用 contentManger.UpdateEditor 方法。为此,您应该实现 IUpdateModel 接口。查看 Orchard.Core/Contents/Controller 上的 AdminController。

顺便说一句,本周末我将发布一个在前端启用自定义编辑器的模块。

于 2013-12-17T20:46:21.510 回答
0

如评论中所述,这将有助于了解您在哪一行出现错误以及哪些项目为空。

话虽如此,要回答您最初的问题,如何从您的代码中创建内容项:

var item = _orchardServices.ContentManager.New("Course");
var part = item.As<CoursePart>();
part.Name = "SomeName";
part.Description = "SomeDescription";
_orchardServices.ContentManager.Create(item);

但是,正如 jmgomez 所说,您通常不必这样做。我建议您阅读文档的这一部分:http: //docs.orchardproject.net/Documentation/Writing-a-content-part

并且可能看看这个教程: http ://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-1

如需使用寻呼机的帮助,您可能需要查看以下内容: http ://skywalkersoftwaredevelopment.net/blog/writing-an-orchard-webshop-module-from-scratch-part-10

于 2014-04-28T07:26:00.190 回答