1. サイトビルド管理

カテゴリページの作成

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

カテゴリはSitecore内のコンテンツアイテムであり、カテゴリに関連付けられたデータはアイテムから取得できます。カテゴリには多くの子が含まれる場合があり、カテゴリ情報の結果をユーザーにページングし、検索を使用して製品とカテゴリの子を取得することを検討してください。

次の前提条件を満たします。

コンテンツ エディターで、次の操作を行います。

  1. カテゴリのビューとして _Category.cshtmlを作成し、Sitecoreのレイアウト コントロールで使用します。カテゴリページに表示される関係のビューは、_CategorySummary.cshtmlというものが必要です。

    カテゴリ.cshtml

    @model CategoryViewModel
    @{
        ViewBag.Title = Model.DisplayName;    
    }
     
    <div class="row">
    <h1>@Model.DisplayNameRender</h1>
        <p>@Model.DescriptionRender</p>
    </div>
    <div>
        <ul class="thumbnails">
            @foreach (var product in Model.ChildProducts)
            {   
                <li class="span3">
                    @Html.Partial("~/Views/Catalog/_ProductSummary.cshtml", new ProductViewModel(product))
                </li>  
            }
        </ul>
    </div>

    製品概要.cshtml

    @model ProductViewModel
    @{
        var mod = (ProductViewModel)Model;
    }
     
    <a href="@Sitecore.Links.LinkManager.GetDynamicUrl(mod.Item)">
        <div class="thumbnail">
            <img src="@mod.FirstImagePath(ImageSize.Detail)" alt="">
            <div class="productLabel">
                <span class="label label-success">@mod.ListPriceRender</span><span class="label label-productName">@mod.DisplayNameRender</span>
            </div>
            <div class="detailsFloatOver">
                    <h4>@mod.DisplayNameRender </h4>
                    <p>@mod.ListPriceRender</p>
            </div>
        </div>
    </a>
  2. カテゴリの基本ビュー モデルを作成します。

        public class CategoryViewModel : Sitecore.Mvc.Presentation.RenderingModel, IImageEntity
        {
            private Item _item;
     
            public CategoryViewModel() { }
            public CategoryViewModel(Item item) { this._item = item; }
     
            public override Item Item
            {
                get
                {
                    if (this._item == null)
                        return base.Item;
     
                    return this._item;
                }
            }
     
            public string DisplayName { get { return this.Item.DisplayName; } }
            public HtmlString DisplayNameRender
            {
                get
                {
                    return PageContext.Current.HtmlHelper.Sitecore().Field(Sitecore.FieldIDs.DisplayName.ToString(), this.Item);
                }
            }
     
            public string Description
            {
                get { return this.Item["Description"]; }
                set { this.Item["Description"] = value; }
            }
     
            public HtmlString DescriptionRender
            {
                get
                {
                    return PageContext.Current.HtmlHelper.Sitecore().Field("Description", this.Item);
                }
            }
     
            public string ImageFilename
            {
                get { return this.Item["Image_filename"]; }
                set { this.Item["Image_filename"] = value; }
            }
     
            public List<Item> ChildProducts { get; protected set; }
            public List<Item> ChildCategories { get; protected set; }
     
            [System.Xml.Serialization.XmlIgnore]
            protected ViewContext CurrentViewContext { get { return ContextService.Get().GetCurrentOrDefault<ViewContext>(); } }
     
            public void Initialize(Rendering rendering, List<Item> products, List<Item> childCategories)
            {
                base.Initialize(rendering);
     
                if (products != null && products.Count > 0)
                    this.ChildProducts = products;
     
                if (childCategories != null && childCategories.Count > 0)
                {
                    this.ChildCategories = childCategories;
                }
            }
        }
    
    
    
  3. アクションコントローラーを作成します。

    public ActionResult Category()
            {
                var viewModel = GetCategoryViewModel(this.Item, this.CurrentRendering);
                return View(this.CurrentRenderingView, viewModel);
            }
    
            protected virtual CategoryViewModel GetCategoryViewModel(Item categoryItem, Rendering rendering)
            {
                if (HttpContext.Items[_CurentCategoryViewModelKeyName] == null)
                {
                    var categoryViewModel = new CategoryViewModel();
                    var childProducts = categoryItem.Children.ToList();
                    categoryViewModel.Initialize(rendering, childProducts, new List<Item>());
                    HttpContext.Items[_CurentCategoryViewModelKeyName] = categoryViewModel;
                }
     
                var viewModel = (CategoryViewModel)HttpContext.Items[_CurentCategoryViewModelKeyName];
                return viewModel;
            }

Sitecoreコンテンツエディタ内からカテゴリをプレビューすると、そのカテゴリの詳細が記載されたページが表示されます。

この記事を改善するための提案がある場合は、 お知らせください!