6

我创建了一个内容部分,并将其添加到内容类型。当我创建内容类型时,不显示内容部分,这与其他字段不同。没有编译或记录错误,内容部分已添加到placement.info。

司机:

[UsedImplicitly]
public class TradeItemPartDriver: ContentPartDriver<TradeItemPart>
{
    private readonly ITradeItemService _tradeItemService;

    private const string TemplateName = "Parts/TradeItem";

    public TradeItemPartDriver(ITradeItemService tradeItemService)
    {
        _tradeItemService = tradeItemService;
    }

    protected override string Prefix
    {
        get { return "TradeItem"; }
    }

    protected override DriverResult Display(
        TradeItemPart part,
        string displayType,
        dynamic shapeHelper)
    {

        return ContentShape("Parts_TradeItem",
                        () => shapeHelper.Parts_TradeItem(
                            ContentPart: part)); 
    }

    protected override DriverResult Editor(
        TradeItemPart part,
        dynamic shapeHelper)
    {
        var shape = ContentShape("Parts_TradeItem_Edit",
                () => shapeHelper.EditorTemplate(
                    TemplateName: TemplateName,
                    Model: BuildEditorViewModel(part),
                    Prefix: Prefix));
        return shape;
    }

    protected override DriverResult Editor(
        TradeItemPart part,
        IUpdateModel updater,
        dynamic shapeHelper)
    {

        var model = new EditTradeItemViewModel();
        updater.TryUpdateModel(model, Prefix, null, null);

        if (part.ContentItem.Id != 0)
        {
            _tradeItemService.UpdateTradeItemForContentItem(
                part.ContentItem, model);
        }

        return Editor(part, shapeHelper);
    }

    private EditTradeItemViewModel BuildEditorViewModel(TradeItemPart part)
    {
        var model = new EditTradeItemViewModel
        {
Code = part.Code, Cost= part.Cost, Unit = part.Unit, Weight = part.Weight,
UnitsList = _tradeItemService.GetUnitTypes()
        };
        return model;
    }
}

处理程序:

public class TradeItemPartHandler : ContentHandler
{
    public TradeItemPartHandler(IRepository<TradeItemPartRecord> repository)
    {
        Filters.Add(StorageFilter.For(repository));
    }
}

楷模:

public enum EnumTradeItemUnit  : byte
{
    Шт = 0,
    Кг = 1,
    Литр = 2,
    Упаковка = 3
}

public class TradeItemPartRecord : ContentPartRecord
{
    public virtual string Code { get; set; }
    public virtual decimal Weight { get; set; }
    public virtual decimal Cost { get; set; }
    public virtual int Unit { get; set; }
}

public class TradeItemPart : ContentPart<TradeItemPartRecord>
{
    public virtual string Code { get; set; }
    public virtual decimal Weight { get; set; }
    public virtual decimal Cost { get; set; }
    public virtual EnumTradeItemUnit Unit { get; set; }
}

服务:

public interface ITradeItemService: IDependency
{
    //IEnumerable<TradeItemPart> GetTradeItems();
    SelectList GetUnitTypes();
    void UpdateTradeItemForContentItem(
        ContentItem item,
        EditTradeItemViewModel model);
}

public class TradeItemService : ITradeItemService
{
    private readonly IContentManager _contentManager;
    private readonly IRepository<TradeItemPartRecord> _tradeItemsRepository;

    public TradeItemService(IContentManager contentManager, IRepository<TradeItemPartRecord> tradeItemsRepository)
    {
        _contentManager = contentManager;
        _tradeItemsRepository = tradeItemsRepository;
    }

    public SelectList GetUnitTypes()
    {
        return new SelectList(
        Enum.GetValues(typeof(EnumTradeItemUnit)).Cast<EnumTradeItemUnit>().Select
            (it =>
            new SelectListItem() { Value = ((byte)it).ToString(), Text = it.ToString() }));
    }

     public void UpdateTradeItemForContentItem(
        ContentItem item,
        EditTradeItemViewModel model)
    {
        var part = item.As<TradeItemPart>();
        part.Code = model.Code;
        part.Cost = model.Cost;
        part.Unit = model.Unit;
        part.Weight = model.Weight;
    }
}

视图模型:

public class EditTradeItemViewModel
{
    [StringLength(10), WebDisplayName("Код")]
    public virtual string Code { get; set; }
    [Required, Range(0, 100, ErrorMessage = "Товары тяжелее 100кг пусть более крутые ребята возят"), WebDisplayName("Вес")]
    public virtual decimal Weight { get; set; }
    [Required, Range(0.01, 100000, ErrorMessage = "Товары дороже 100тыс.р. пусть более крутые ребята возят"), WebDisplayName("Цена")]
    public virtual decimal Cost { get; set; }
    [Required, WebDisplayName("Единица измерения")]
    public virtual EnumTradeItemUnit Unit { get; set; }
    public virtual SelectList UnitsList { get; set; }
}

~/Views/EditorTemplates/Parts/TradeItem.cshtml

@model Delivery.ViewModels.EditTradeItemViewModel
<fieldset>
    <legend>@T("Характеристики")</legend>

  <div class="editor-label">
    @Html.LabelFor(model => model.Code, T("Код"))
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Code)
    @Html.ValidationMessageFor(model => model.Code)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Unit, T("Единица измерения"))
  </div>
  <div class="editor-field">
    @Html.DropDownListFor(model => model.Unit, Model.UnitsList)
    @Html.ValidationMessageFor(model => model.Unit)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Cost, T("Стоимость"))
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Cost)
    @Html.ValidationMessageFor(model => model.Cost)
  </div>

  <div class="editor-label">
    @Html.LabelFor(model => model.Weight, T("Вес одной единицы"))
  </div>
  <div class="editor-field">
    @Html.TextBoxFor(model => model.Weight)
    @Html.ValidationMessageFor(model => model.Weight)
  </div>

</fieldset>

放置:

<Placement>
  <Place Parts_TradeItem_Edit="Content:10"/>
  <Place Parts_TradeItem="Content:10"/>
</Placement>

迁移:

        SchemaBuilder.CreateTable("TradeItemRecord",
                       table => table
                           .ContentPartRecord()
                           .Column<string>("Code", column => column.WithLength(10))
                           .Column<decimal>("Weight", column => column.WithPrecision(8).WithScale(2))
                           .Column<decimal>("Cost", column => column.WithPrecision(8).WithScale(2))
                           .Column<byte>("Unit")
                       );

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

1 回答 1

12

您的代码通常存在两个问题。

首先,在您的迁移中,您调用了该部分TradeItem,而在模型中,您调用了它TradeItemPart。更改其中一个将解决编辑器中为什么没有显示的问题。

这导致我们遇到第二个问题,它将禁用保存您的任何数据。你没有连接你的TradeItemPart和你的TradeItemPartRecord。要解决此问题,您需要进行调整TradeItemPart,使其看起来像这样:

public class TradeItemPart : ContentPart<TradeItemPartRecord>
{
    public string Code { 
        get { return Record.Code; } 
        set { Record.Code = value; } 
    }
    public decimal Weight { 
        get { return Record.Weight; } 
        set { Record.Weight = value; } 
    }
    public decimal Cost { 
        get { return Record.Cost; } 
        set { Record.Cost = value; } 
    }
    public EnumTradeItemUnit Unit { 
        get { return (EnumTradeItemUnit)Record.Unit; } 
        set { Record.Unit = (int)value; } 
    }
}

我也不确定 Orchard 是否会正确地将byte类型列与类型相关联int。如果上述更改不起作用,请尝试将 column 的列类型更改Unitint.

于 2012-11-05T20:47:48.327 回答