0

我正在创建一个自定义部件,它将使用管理员 UI 的剔除保存一个 IEnumerable 日期列表。

一切正常,除了我不确定如何获取发布的 json 数据,以推送到数据库中......

模型

namespace HappyCity.Parts.Models {

    [OrchardFeature("HappyCity.Parts.ImportantDates")]
    public class ImportantDateListRecord : ContentPartRecord {
        public virtual string Title { get; set; }
    }

    [OrchardFeature("HappyCity.Parts.ImportantDates")]
    public class ImportantDateListPart : ContentPart<ImportantDateListRecord> {

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

        public string JsonDates { get; set; }


        private readonly LazyField<IEnumerable<ImportantDateRecord>> _importantDates = new LazyField<IEnumerable<ImportantDateRecord>>();
        public LazyField<IEnumerable<ImportantDateRecord>> ImportantDatesField { get { return _importantDates; } }
        public IEnumerable<ImportantDateRecord> ImportantDates
        {
            get { return _importantDates.Value; }
        }

    }

    [OrchardFeature("HappyCity.Parts.ImportantDates")]
    public class ImportantDateRecord 
    {
        [JsonProperty("id")]
        public virtual int Id { get; set; }

        [JsonProperty("title")]
        public virtual string Title { get; set; }

        [JsonProperty("date")]
        public virtual DateTime? Date { get; set; }

        [JsonProperty("description")]
        public virtual string Descripiton { get; set; }
        //public virtual string Link { get; set; }
    }

}

移民

 public class Migrations : DataMigrationImpl {

        public int Create() {
            // Creating table ImportantDatesRecord
            SchemaBuilder.CreateTable(typeof(ImportantDateListRecord).Name, table => table
                .ContentPartRecord()
                .Column<string>("Title")
            );

            // make the date list part attachable
            ContentDefinitionManager.AlterPartDefinition(typeof(ImportantDateListPart).Name,
                builder => builder.Attachable());


            // Creating table ImportantDateRecord
            SchemaBuilder.CreateTable("ImportantDateRecord", table => table
                .Column("Id", DbType.Int32, c => c.Identity())
                .Column("Title", DbType.String)
                .Column("Date", DbType.DateTime)
                .Column("Descripiton", DbType.String)
            );


            return 1;
        }


    }

司机

namespace HappyCity.Parts.Drivers {

    [OrchardFeature("HappyCity.Parts.ImportantDates")]
    public class ImportantDatesDriver: ContentPartDriver<ImportantDateListPart> {

        // This prefix will be used to distinguish between similarly named input fields when building the editor form
        protected override string Prefix
        {
            get { return "HappyCity.Parts.ImportantDates.ImportantDateListPart"; }
        }

        // This method gets called when building the display shape of the content item the part is attached to.
        protected override DriverResult Display(ImportantDateListPart part, string displayType, dynamic shapeHelper)
        {
            return ContentShape("Parts_ImportantDates",
                () => shapeHelper.Parts_ImportantDates(DisplayType: displayType));
        }

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

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

        //POST
        protected override DriverResult Editor(
            ImportantDateListPart part, IUpdateModel updater, dynamic shapeHelper) {

            updater.TryUpdateModel(part, Prefix, null, null);

            return Editor(part, shapeHelper);
        }

    }


}

处理程序

public ImportantDatesHandler(IRepository<ImportantDateListRecord> repository , Work<IImportantDatesManager> importantDatesManager)
        {
            Filters.Add(StorageFilter.For(repository));

            OnActivated<ImportantDateListPart>((context, part) =>
            {
                part.ImportantDatesField.Loader(() => importantDatesManager.Value.GetDates());
            });


            OnUpdating<ImportantDateListPart>((context, part) =>
            {

                var JsonDates = part.JsonDates;
                // ***** this just gives me the current dates from the db not the ones poseted in the form. 


                // ***** if i create a list of dates like this they will be saved to the database
                //var dates = new List<ImportantDateRecord>();
                //dates.Add(new ImportantDateRecord
                //{
                //    Title = "test date",
                //    Date = new DateTime(1977, 8, 15),
                //    Descripiton = "lorem ipsum blur"
                //});


                //foreach (var importantDateRecord in JsonDates)
                //{
                //    importantDatesManager.Value.SaveDate(importantDateRecord);
                //}

            });

        }

服务

 public interface IImportantDatesManager : IDependency
    {
        IEnumerable<ImportantDateRecord> GetDates();
        IEnumerable<ImportantDateRecord> GetDates(int maxCount);
        void SaveDate(ImportantDateRecord importantDate);

    }

    [OrchardFeature("HappyCity.Parts.ImportantDates")]
    public class ImportantDatesManager : IImportantDatesManager {

        private readonly IRepository<ImportantDateRecord> _importantDatesRepostiory;


        public ImportantDatesManager(IRepository<ImportantDateRecord> importantDatesRepostiory) {
            _importantDatesRepostiory = importantDatesRepostiory;
        }


        public IEnumerable<ImportantDateRecord> GetDates() {
            return _importantDatesRepostiory.Table.AsEnumerable();
        }

        public IEnumerable<ImportantDateRecord> GetDates(int maxCount) {
            return _importantDatesRepostiory.Table.Take(maxCount);
        }

        public void SaveDate(ImportantDateRecord importantDate) {
            // Let's also practice exception handling.
            if (String.IsNullOrEmpty(importantDate.Title)) throw new ArgumentNullException("importantDate", "Title was null");

            var date = _importantDatesRepostiory.Fetch(record => record.Id == importantDate.Id).FirstOrDefault();

            if (date == null)
            {
                date = new ImportantDateRecord();
                _importantDatesRepostiory.Create(date);
            }

            date.Title = importantDate.Title;
            date.Date = importantDate.Date;
            date.Descripiton = importantDate.Descripiton;
        }
    }

在处理程序的某个地方,我应该能够获取请求中发布的新值。但是如何?

同样在服务器上的 SaveDate 方法中,我不需要 _importantDatesRepostiroy.update 吗?

4

1 回答 1

0

您想使用OnUpdated处理程序,而不是OnUpdating处理程序。

OnUpdating发生在每个 Content Part Driver 的Editor()方法 (POST) 被调用之前,然后OnUpdated发生。内容部分驱动程序的Editor()方法是您执行IUpdater.TryUpdateModel()调用的地方,您可以使用该方法用表单中的数据填充内容部分。

通过等待OnUpdated处理程序,该部分现在包含JsonDates您从视图中发布的字符串值。

关于是否需要调用的问题IRepository.Update(),大多数情况下不需要,因为整个请求都有一个隐式事务,并且您的更改将在请求结束时保存到数据库中。

于 2013-11-21T14:28:17.270 回答