Orchard CMS保存其他JSON数据

斧头

我正在创建一个自定义部件,该部件将使用管理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吗?

大室克幸

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

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

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

覆盖 Orchard CMS 中的 Orchard Setup 视图

来自分类Dev

Orchard CMS多种布局

来自分类Dev

将松弛与Orchard CMS集成

来自分类Dev

在Orchard CMS中访问HttpConfiguration

来自分类Dev

在Orchard CMS中访问HttpConfiguration

来自分类Dev

Orchard:使用InfosetPart在Orchard_framework_contentitemversionrecord中存储“数据”

来自分类Dev

Orchard:使用InfosetPart在Orchard_framework_contentitemversionrecord中存储“数据”

来自分类Dev

Orchard CMS 日志数据存储在数据库中?

来自分类Dev

Orchard CMS自定义表格

来自分类Dev

Orchard CMS零件放置

来自分类Dev

在Orchard CMS中动态添加标题

来自分类Dev

来自Controller的Orchard CMS ResizeMedia网址。

来自分类Dev

使用Orchard CMS复制Bootstrap主题

来自分类Dev

Orchard CMS自定义后台作业

来自分类Dev

Orchard CMS-博客文章精选图片

来自分类Dev

Orchard CMS订单项(按ID)

来自分类Dev

Orchard CMS默认传呼机问题

来自分类Dev

Orchard CMS:资源获取404(未找到)

来自分类Dev

使用 Orchard CMS,favicon 在哪里设置?

来自分类Dev

Orchard Widget数据存储在哪里?

来自分类Dev

与其他基于Asp.net的CMS相比,Orchard“ Layers”概念似乎是一个奇怪的概念

来自分类Dev

Orchard 1.7.2自定义表单不会保存数据

来自分类Dev

Orchard 1.7.2自定义表单不会保存数据

来自分类Dev

如何将Orchard CMS Admin控件内联

来自分类Dev

Orchard CMS:以字符串形式获取帖子网址

来自分类Dev

如何将Live Writer连接到Orchard CMS

来自分类Dev

在Orchard CMS中将MediaPicker字段迁移到MediaLibraryPicker

来自分类Dev

Orchard CMS-删除发布日期和时间页面

来自分类Dev

您如何仅在Orchard CMS中获得图像URL?

Related 相关文章

热门标签

归档