1

我需要在小部件视图中访问 mediapickerfield。该字段与“ActionPart”相关。

当我创建多对多关系以在小部件中显示我的“ActionPart”时,我遇到了问题。

当我映射多对多时,我采用了“ActionPart”,但无法访问 mediapicker 字段。

记录类

public class ActionPartRecord : ContentPartRecord
{
    public virtual string Text { get; set; }
    public virtual double Price { get; set; }
    public virtual int TextPosX { get; set; }
    public virtual int TextPosY { get; set; }
    public virtual String TextSale { get; set; }
    public virtual int Color { get; set; }
}

public class ActionListRecord
{
    public virtual int Id { get; set; }
    public virtual ActionPartRecord ActionPartRecord { get; set; }
    public virtual ActionWidgetPartRecord ActionWidgetPartRecord { get; set; }
}

public class ActionWidgetPartRecord : ContentPartRecord
{
    public ActionWidgetPartRecord()
    {
        ActionList = new List<ActionListRecord>();
    }
    public virtual IList<ActionListRecord> ActionList { get; set; }
}

public class ActionWidgetPart : ContentPart<ActionWidgetPartRecord>
{
    public IEnumerable<ActionPartRecord> ActionList
    {
        get
        {
            return Record.ActionList.Select(x => x.ActionPartRecord);
        }
    }
}

ActionPart 类

public class ActionPart : ContentPart<ActionPartRecord>
{
    public String Text
    {
        get { return Record.Text; }
        set { Record.Text = value; }
    }
    /*other field*/
}

迁移

public int Create()
{
    SchemaBuilder.CreateTable("ActionPartRecord", table => table
        .ContentPartRecord()
        .Column<string>("Text")
        .Column<double>("Price")
        .Column<int>("TextPosX")
        .Column<int>("TextPosY")
        .Column<string>("TextSale")
        .Column<int>("Color")
        );

    ContentDefinitionManager.AlterPartDefinition("ActionPart",
     builder => builder
         .WithField("BaseImage", fieldBuilder => fieldBuilder.OfType("MediaPickerField").WithDisplayName("Action Image"))
         .WithField("PatternImage", fieldBuilder => fieldBuilder.OfType("MediaPickerField").WithDisplayName("Pattern Image"))
         .WithField("TimeExp", fieldBuilder => fieldBuilder.OfType("DateTimeField").WithDisplayName("Expecting date"))
    .Attachable());

    ContentDefinitionManager.AlterTypeDefinition("Action", cfg => cfg
      .WithPart("CommonPart")
      .WithPart("TitlePart")
      .WithPart("RoutePart")
      .WithPart("BodyPart")
      .WithPart("ActionPart")
      .Creatable()
      .Indexed());

    SchemaBuilder.CreateTable("ActionListRecord",
       table => table
           .Column<int>("Id", column => column.PrimaryKey().Identity())
           .Column<int>("ActionPartRecord_Id")
           .Column<int>("ActionWidgetPartRecord_Id")
       );

    SchemaBuilder.CreateTable("ActionWidgetPartRecord", table => table
      .ContentPartRecord()
      );

    ContentDefinitionManager.AlterPartDefinition(
        "ActionWidgetPart",
        builder => builder.Attachable());

    ContentDefinitionManager.AlterTypeDefinition("ActionWidget", cfg => cfg
        .WithPart("ActionWidgetPart")
        .WithPart("WidgetPart")
        .WithPart("CommonPart")
        .WithSetting("Stereotype", "Widget"));
    return 1;
}

驱动显示方式

protected override DriverResult Display(
    ActionWidgetPart part,
    string displayType,
    dynamic shapeHelper)
{
    return ContentShape("Parts_ActionWidget",
                    () => shapeHelper.Parts_ActionWidget(
                        ContentPart: part,
                        ActionList: part.ActionList));
}

小部件视图

@foreach (var action in Model.ActionList)
{
    <div class="item">

        *How to access BaseImage Field in this row*

        <div class="sale-pattern"></div>
        <div class="container">
            <div class="carousel-caption">
                <h1>@action.Text</h1>
                <h1 class="price">@action.Price</h1>
            </div>
        </div>
    </div>
}
4

1 回答 1

1

我很喜欢下一部分代码的问题:

@foreach (var action in Model.ActionList)
{
var contentId = (int)action.Id;
var contentManager = WorkContext.Resolve<IContentManager>();
dynamic contentItem = contentManager.Get(contentId).As<ActionPart>();

    <div class="item">
    <img src="@(((string)contentItem.BaseImage.Url).Replace("~/",""))" alt="">
    <div class="sale-pattern"></div>
    <div class="container">
        <div class="carousel-caption">
            <h1>@action.Text</h1>
            <h1 class="price">@action.Price</h1>
        </div>
    </div>
</div>
}
于 2012-11-13T11:32:38.463 回答