在Orchard CMS中通过代码创建内容项

时间:2013-12-17 12:52:59

标签: orchardcms orchardcms-1.7

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

“对象引用未设置为对象的实例。”

[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>

2 个答案:

答案 0 :(得分:0)

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

本周末,我将发布一个模块,该模块可以在前端启用自定义编辑器。

答案 1 :(得分: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