我在理解如何在Sitecore EXM電子郵件組件中顯示鏈接時遇到一些困難。呈現組件MVC Sitecore中的鏈接列表
我正在使用Sitecore MVC控制器呈現。我本質上是試圖複製主節組件,它是Sitecore EXM平臺的默認組件。
於是開始與我有一個控制器MainSectionMVCController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestSitecore.Repositories;
namespace TestSitecore.Controllers
{
public class MainSectionMVCController : Controller
{
// GET: MainSectionMVC
public ActionResult Index()
{
var repository = new MainSectionMVCRepository();
var mainSectionMVC = repository.GetMainSectionMVC();
var mainSectionMVCViewModel = repository.GetMainSectionMVCViewModel(mainSectionMVC);
return View(mainSectionMVCViewModel);
}
}
}
我有一個倉庫MainSectionMVCRepository.cs:
using Sitecore.Links;
using Sitecore.Mvc.Presentation;
using Sitecore.Web.UI.WebControls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestSitecore.Models;
using TestSitecore.Models.MVCEmailModels;
using Sitecore.Data.Items;
using Sitecore.Collections;
namespace TestSitecore.Repositories
{
public class MainSectionMVCRepository
{
public MainSectionMVC GetMainSectionMVC()
{
var mainSectionMVC = new MainSectionMVC();
var rendering = RenderingContext.Current.Rendering;
var dataSource = rendering.Item;
mainSectionMVC.Title = new HtmlString(FieldRenderer.Render(dataSource, Constants.FIELD_TITLE));
mainSectionMVC.Text = new HtmlString(FieldRenderer.Render(dataSource, Constants.FIELD_TEXT));
mainSectionMVC.Image = new HtmlString(FieldRenderer.Render(dataSource, Constants.FIELD_IMAGE));
mainSectionMVC.Links = GetLinks(dataSource);
return mainSectionMVC;
}
private List<Item> GetLinks(Item dataSource)
{
List<Item> links = new List<Item>();
// I need to get access the the Links that are attached to the datasource item ???? How to do this
return links;
}
public MainSectionMVCViewModel GetMainSectionMVCViewModel(MainSectionMVC mainSectionMVC)
{
var viewModel = new MainSectionMVCViewModel();
var rendering = RenderingContext.Current.Rendering;
//viewModel.Background = rendering.Parameters["Background"];
viewModel.ContextItem = PageContext.Current.Item;
viewModel.MainSectionMVC = mainSectionMVC;
return viewModel;
}
}
}
和模型MainSectionMVC.cs:
using Sitecore.Data.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestSitecore.Models.MVCEmailModels
{
public class MainSectionMVC
{
public HtmlString Title { get; set; }
public HtmlString Image { get; set; }
public HtmlString Text { get; set; }
public List<Item> Links { get; set; }
}
}
我有一個ViewModel MainSectionMVCViewModel.cs:
using Sitecore.Data.Items;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace TestSitecore.Models.MVCEmailModels
{
public class MainSectionMVCViewModel
{
public MainSectionMVC MainSectionMVC { get; set; }
public string Background { get; set; }
public Item ContextItem { get; set; }
}
}
最後我有一個連接到控制器的索引動作Index.cshtml:
@using Sitecore.Mvc
@using Sitecore.Data.Items
@using Sitecore.Web.UI.WebControls;
@using TestSitecore.Models
@using TestSitecore.Models.MVCEmailModels
@model MainSectionMVCViewModel
<div class="highlight">
<div class="row">
<div class="col-md-2 col-lg-2"></div>
<div class="col-md-5 col-lg-5">
<h2>@Model.MainSectionMVC.Title</h2>
</div>
<div class="col-md-3 col-lg-3" style="align-content: center">
@Model.MainSectionMVC.Image
</div>
<div class="col-md-2 col-lg-2"></div>
</div>
<div class="row">
<div class="col-md-8 col-lg-8">
<h2>@Model.MainSectionMVC.Text</h2>
</div>
<div class="col-md-4 col-lg-4"></div>
</div>
<div class="row">
<!--DISPLAY THE LINKS HERE-->
</div>
</div>
我的問題是如何填充在我的倉庫鏈接列表,然後渲染輸出在我的組件上。
從Sitecore中的鏈接項查看模板顯示數據定義包含2個字段,即文本和目標。
所以我假設文本字段用作鏈接的顯示文本,而目標實際上是ItemPath/ItemID。
我試過使用var children = dataSource.GetChildren();
這使我得到父項(主要部分)的所有孩子。目前只有鏈接可以是主要部分項目的子項目,但是如果組件模板被修改,這可能會改變。
我努力尋找一種合適的方式來測試子項是否是鏈接。所以任何關於測試物品類型的提示都會被讚賞。
任何想法?