天天看點

Orchard子產品開發全接觸3:分類的實作及内容呈現(Display)

一:分類用現有技術怎麼實作?

1.1 目前這種模式的缺點

這種模式的缺點就是,你要麼查詢 Book ,要麼查詢 DVD,

Orchard子產品開發全接觸3:分類的實作及内容呈現(Display)

不能查詢全部的 Product,這樣一來,我們又要自己寫代碼了。

二:更新 Module.txt

因為我們的子產品依賴一個特性, Orchard.Projections,是以,修改為:

name: tminji.shop antiforgery: enabled author: tminji.com version: 1.0.0 orchardversion: 1.0.0 description: The tminji.com module is a shopping module.  Dependencies: Orchard.Projections features:     shop:         Description: shopping module.         Category: ASample

三:建立 Filter

然後,

1:增加 Filters 檔案夾;

2:建立 ProductPartFilter.cs,如下:

using Orchard.Localization; using Orchard.Mvc.Filters; using Orchard.Projections.Descriptors.Filter; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TMinji.Shop.Models; namespace TMinji.Shop.Filters {     public class ProductPartFilter : Orchard.Projections.Services.IFilterProvider     {         public Localizer T { get; set; }         public ProductPartFilter()         {             T = NullLocalizer.Instance;         }         public void Describe(DescribeFilterContext describe)             describe.For(                 "Content",          // The category of this filter                 T("Content"),       // The name of the filter (not used in 1.4)                 T("Content"))       // The description of the filter (not used in 1.4)                 // Defines the actual filter (we could define multiple filters using the fluent syntax)                 .Element(                     "ProductParts",     // Type of the element                     T("Product Parts"), // Name of the element                     T("Product parts"), // Description of the element                     ApplyFilter,        // Delegate to a method that performs the actual filtering for this element                     DisplayFilter       // Delegate to a method that returns a descriptive string for this element                 );         private void ApplyFilter(FilterContext context)             // Set the Query property of the context parameter to any IHqlQuery. In our case, we use a default query             // and narrow it down by joining with the ProductPartRecord.             context.Query = context.Query.Join(x => x.ContentPartRecord(typeof(ProductPartRecord)));         private LocalizedString DisplayFilter(FilterContext context)             return T("Content with ProductPart");     } }

現在,在背景,就可以看到這個 Filter 了,如下:

Orchard子產品開發全接觸3:分類的實作及内容呈現(Display)

現在,我們增加這個 filter,就可以得到結果了,如下:

Orchard子產品開發全接觸3:分類的實作及内容呈現(Display)

我們添加 Projection(不再贅述),然後在前台顯式出來:

Orchard子產品開發全接觸3:分類的實作及内容呈現(Display)

四:内容呈現(Dispaly)

但是,我們發現一個問題,就是 Price 和 SKU 并沒有呈現出來,包括我們點選 More ,也并沒有出現這些我們的核心資料。

還記得什麼沒有,我們在背景建立 Book 或者 DVD 的時候,一開始根本沒有儲存上,是因為我們沒有在 Driver 中存在傳回 DriverResult 的方法,以及定義對應的 cshtml 檔案,現在,讓我們來完成這件事情。

首先,修改 ProductPartDriver 類,增加方法:

protected override DriverResult Display(ProductPart part, string displayType, dynamic shapeHelper)     return ContentShape("Parts_Product", () => shapeHelper.Parts_Product(             Price: part.UnitPrice,             Sku: part.Sku         ));

前台在呈現含有 ProductPart 的頁面的時候,會調用這個 Display 方法。根據這個方法,我們知道,建立了一個 Parts_Product 的 shape,它對應的 cshtml 檔案是:

Views/Parts/Product.cshtml

現在,我們來建立這個檔案:

@{     var price = (decimal)Model.Price;     var sku = (string)Model.Sku; <article>     Price: @price<br />     Sku: @sku </article>

然後,記住,修改我們的 placement.info:

<Placement>   <Place Parts_Product_Edit="Content:1" />   <Place Parts_Product="Content:0" /> </Placement>

大功告成,見:

Orchard子產品開發全接觸3:分類的實作及内容呈現(Display)

本文轉自最課程陸敏技部落格園部落格,原文連結:http://www.cnblogs.com/luminji/p/3858977.html,如需轉載請自行聯系原作者