【问题标题】:Render a list of links in my Component MVC Sitecore在我的 Component MVC Sitecore 中呈现链接列表
【发布时间】:2016-11-16 05:06:37
【问题描述】:

我在理解如何在 Sitecore EXM 电子邮件组件中显示链接时遇到了一些困难。

我正在使用 Sitecore MVC 控制器渲染。我实际上是在尝试复制 Sitecore EXM 平台默认提供的 Main Section 组件。

首先我有一个控制器 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 链接到控制器的 Index 操作:

@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 个字段,文本和目标。

所以我假设 Text 字段用作链接的显示文本,而目标实际上是 ItemPath/ItemID。

我尝试使用var children = dataSource.GetChildren(); 这让我获得了父项(主要部分)的所有子项。目前只有 Links 可以是 Main Section Item 的子项,但是如果修改了组件模板,这可能会发生变化。

我努力寻找一种合适的方法来测试子项是否是链接。因此,任何有关测试项目类型的提示也将不胜感激。

有什么想法吗?

【问题讨论】:

    标签: model-view-controller hyperlink sitecore sitecore-mvc sitecore-exm


    【解决方案1】:

    所以在经历了很多错误的转折和困境之后,我终于设法找到了解决方案。 我想我错过了对 Sitecore HTML 帮助程序类的一些重要理解。

    所以对于那些在理解这里有类似问题的人来说,我是如何解决这个问题的。

    在我的 MainSectionMVCRepository 类中,我完成了 GetLinks() 方法,如下所示:

    private List<Item> GetLinks(Item item)
    {
        ChildList children = item.GetChildren();
        List<Item> links = new List<Item>(children.Count);
    
        foreach (Item link in children)
        {
            links.Add(link);
        }
    
        return links;
    }
    

    然后在我的 Index.cshtml 文件中,我使用以下代码呈现链接:

     @{  if (Model.MainSectionMVC.Links.Count > 0)
        {
            <ul>
                @foreach (Item link in @Model.Links)
                {
                    <li>
                        @Html.Sitecore().BeginField("Destination", link, new { haschildren = true })
                        @Html.Sitecore().Field("Text", link)
                        @Html.Sitecore().EndField()
                    </li>
                }
            </ul>
        }
    }
    

    我对结果很满意。以这种方式呈现时,链接会在页面编辑器中显示和编辑。

    希望这可以帮助其他遇到类似问题的人:D

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多