Orchard CMS - 处理程序不更新数据库

时间:2012-03-30 19:30:33

标签: asp.net-mvc data-binding razor orchardcms

在我的果园模块中,一切正常,但不幸的是数据库没有更新。

我已在 Orchard.Web \ Modules \ Course \ Views \ EditorTemplates \ Parts \ Course.cshtml 中为管理面板定义了视图,如下所示:

@model Course.Models.CoursePart

<fieldset>
  <legend>Course Fields</legend>

  <div class="editor-label">
    @Html.LabelFor(model => model.Title)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Title)
    @Html.ValidationMessageFor(model => model.Title)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Description)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Description)
    @Html.ValidationMessageFor(model => model.Description)
  </div>


  <div class="editor-label">
    @Html.LabelFor(model => model.ImagePath)
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.ImagePath)
    @Html.ValidationMessageFor(model => model.ImagePath)
  </div>


</fieldset>

司机如下:

public class CourseDriver : ContentPartDriver<CoursePart>
    {
        protected override DriverResult Display(CoursePart part,
                                        string displayType,
                                        dynamic shapeHelper)
        {

            return ContentShape("Parts_Course",
                () => shapeHelper.Parts_Course(
                Description: part.Description,
                Title: part.Title,
                ImagePath: part.ImagePath,
                Area: part.Area
                ));
        }

        //GET
        protected override DriverResult Editor(CoursePart part,
                                               dynamic shapeHelper)
        {

            return ContentShape("Parts_Course_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: "Parts/Course",
                    Model: part,
                    Prefix: Prefix));
        }

        //POST
        protected override DriverResult Editor(CoursePart part,
                                               IUpdateModel updater,
                                               dynamic shapeHelper)
        {
            updater.TryUpdateModel(part, Prefix, null, null);
            return Editor(part, shapeHelper);
        }
    }

和处理程序:

namespace Course.Handlers
{
    public class CourseHandler: ContentHandler
    {
          public CourseHandler(IRepository<CourseRecord> repository) 
          {
                Filters.Add(StorageFilter.For(repository));
          }

    }
}

下面描述的迁移类位于 Orchard.Web \ Modules \ Course \ Migrations.cs 文件中。

namespace Course {
    public class Migrations : DataMigrationImpl {

        public int Create() {
            // Creating table CoursePartRecord
            SchemaBuilder.CreateTable("CourseRecord", table => table
                .ContentPartRecord()
                .Column("Area", DbType.String)
                .Column("Description", DbType.String)
                .Column("Title", DbType.String)
                .Column("ImagePath", DbType.String)
            );

            //Add the AlterPartDefinition lines to the migration in order to make the part
            //attachable to any content type. 
            ContentDefinitionManager.AlterPartDefinition(
                typeof(CoursePart).Name, cfg => cfg.Attachable());

            return 1;
        }

        public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterTypeDefinition("CourseContent", cfg => cfg
              .WithPart("CommonPart")
              .WithPart("RoutePart")
              .WithPart("BodyPart")
              .WithPart("CoursePart")
              .WithPart("CommentsPart")
              .WithPart("TagsPart")
              .WithPart("LocalizationPart")
              .Creatable()
              .Indexed());
            return 2;
        }
    }
}

模型如下所示:

namespace Course.Models
{
    public class CourseRecord : ContentPartRecord
    {
        public virtual String Area { get; set; }

        public virtual String Description { get; set; }
        public virtual String Title { get; set; }

        public virtual String ImagePath { get; set; }

    }


    public class CoursePart : ContentPart<CourseRecord>
    {
        public String Area { get; set; }
        [Required]
        public String Description { get; set; }
        [Required]
        public String Title { get; set; }
        public String ImagePath { get; set; }
    }
}

我按照果园文档中的示例执行了以下步骤:http://docs.orchardproject.net/Documentation/Writing-a-content-part

问题:未创建或更新数据库,记录中的每个属性都使用空值更新,repository.Table方法中的CourseHandler始终返回列表。

祝你好运, 铁托

1 个答案:

答案 0 :(得分:1)

感谢@mdm评论给了我一个提示,我再看一下我的代码迁移代码。 并且类CoursePart被错误地定义,因为它不是从Record中写入或读取的。

当我将其更改为下面的代码时,它正常工作:

public class CoursePart : ContentPart<CourseRecord>

{
    public String Area
    {
        get { return Record.Area; }
        set { Record.Area = value; }
    }

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

    [Required]
    public String Title
    {
        get { return Record.Title; }
        set { Record.Title = value; }
    }

    public String ImagePath
    {
        get { return Record.ImagePath; }
        set { Record.ImagePath = value; }
    }
}