Orchard Simple Widget不保存数据库值

时间:2012-12-22 00:53:54

标签: orchardcms orchardcms-1.6

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

Migration.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;
    }

PartRecord

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);
    }
}

EditorTemplage

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

1 个答案:

答案 0 :(得分: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; }
    }
}