2

我在创建一个简单的小部件时遇到了一点问题。我要做的就是创建一个具有 MediaPickerField 和 2 个 int 和 2 个字符串字段的小部件,这些字段使用部件存储在数据库中。一切都像宣传的那样工作。ContentRecord 被创建并且图像字段被正确存储,除了四个字段没有被存储。日志中没有错误,似乎无法理解为什么会发生这种情况。有没有人遇到过这个?谢谢你。

迁移.cs

public int UpdateFrom1()
    {
        SchemaBuilder.CreateTable("SchoenNavButtonPartRecord",
            table => table.ContentPartRecord()
                          .Column<string>("Url", col=> col.WithLength(2140))
                          .Column<string>("Text")
                          .Column<int>("ButtonWidth")
                          .Column<int>("ButtonHeight"));
        ContentDefinitionManager.AlterTypeDefinition("SchoenNavButton", builder => 
            builder.WithPart("CommonPart")
                   .WithPart("SchoenNavButtonPart")
                   .WithPart("TitlePart")
                   .Creatable());
        ContentDefinitionManager.AlterPartDefinition("SchoenNavButtonPart", builder => 
            builder.WithField("ButtonImage", field=> 
                field.OfType("MediaPickerField")
                    .WithDisplayName("Button Image")
                    .WithSetting("Hint", "Select Image for Button")));
        return 2;
    }

零件记录

public class SchoenNavButtonPartRecord : ContentPartRecord
{
    public virtual string Url { get; set; }
    public virtual string Text { get; set; }
    public virtual int ButtonWidth { get; set; }
    public virtual int ButtonHeight { get; set; }
}

部分

public class SchoenNavButtonPart : ContentPart<SchoenNavButtonPartRecord>
{
    public string Url { get; set; }
    public string Text { get; set; }

    [DefaultValue(296)]
    public int ButtonWidth { get; set; }

    [DefaultValue(188)]
    public int ButtonHeight { get; set; }
}

处理程序

public class SchoenNavButtonHandler : ContentHandler
{
    public SchoenNavButtonHandler(IRepository<SchoenNavButtonPartRecord> buttonImageLinkRepository)
    {
        Filters.Add(StorageFilter.For(buttonImageLinkRepository));
    }

}

司机

public class SchoenNavButtonPartDriver : ContentPartDriver<SchoenNavButtonPart>
{
    public SchoenNavButtonPartDriver()
    {

    }
    protected override string Prefix
    {
        get
        {
            return "SchoenNavButton";
        }
    }
    protected override DriverResult Display(SchoenNavButtonPart part, string displayType, dynamic shapeHelper)
    {
        var fields = ((ContentPart) part).Fields.OfType<MediaPickerField>();
        MediaPickerField mediaPickerField = null;
        if(fields.Any())
        {
            mediaPickerField = fields.ElementAt(0);
        }
        return ContentShape("Parts_SchoenNavButton",
                         () => shapeHelper.Parts_SchoenNavButton(
                             SchoenNavButtonPart: part,
                             ImageUrl: mediaPickerField == null ? "#" : mediaPickerField.Url
                                   ));
    }
    protected override DriverResult Editor(SchoenNavButtonPart part, dynamic shapeHelper)
    {
        return ContentShape("Parts_SchoenNavButton_Edit", () =>
            shapeHelper.EditorTemplate(
                TemplateName: "Parts/SchoenNavButton",
                Model: part,
                Prefix: Prefix));
    }
    protected override DriverResult Editor(SchoenNavButtonPart part, Orchard.ContentManagement.IUpdateModel updater, dynamic shapeHelper)
    {
        updater.TryUpdateModel(part, Prefix, null, null);
        return Editor(part, shapeHelper);
    }
}

编辑器模板

@model FishySoftware.SchoenBuilders.Models.SchoenNavButtonPart

<fieldset>
    <legend>Button Details</legend>
    <div class="editor-label">
        @Html.LabelFor(model => model.Url, T("Url"))
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Url)
        @Html.ValidationMessageFor(model => model.Url)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.Text, T("Text"))
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.Text)
        @Html.ValidationMessageFor(model => model.Text)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.ButtonWidth, T("Button Width"))
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.ButtonWidth)
        @Html.ValidationMessageFor(model => model.ButtonWidth)
    </div>
    <div class="editor-label">
        @Html.LabelFor(model => model.ButtonHeight, T("Button Height"))
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(model => model.ButtonHeight)
        @Html.ValidationMessageFor(model => model.ButtonHeight)
    </div>
</fieldset>
4

1 回答 1

2

Orchard 总是使用Record类的内容来将数据保存到数据库中。另一方面,您正在Part课堂内保存浏览器中的数据,并且您没有正确连接您的PartRecord课堂。

为此,您必须将您的更改ShoenNavButtonPart为:

public class SchoenNavButtonPart : ContentPart<SchoenNavButtonPartRecord>
{
    public string Url { 
        get { return Record.Url; } 
        set { Record.Url = value; }
    }

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

    [DefaultValue(296)]
    public int ButtonWidth { 
        get { return Record.ButtonWidth; } 
        set { Record.ButtonWidth = value; }
    }

    [DefaultValue(188)]
    public int ButtonHeight { 
        get { return Record.ButtonHeight; } 
        set { Record.ButtonHeight = value; }
    }
}
于 2012-12-22T15:20:19.397 回答