Sitecore将所有项目加载到MVC模型中?

时间:2015-08-31 20:35:14

标签: sitecore glass-mapper sitecore-mvc sitecore8

我创建了一系列自定义模板来存储Sitecore中的项目(例如IndustriesSubindustries等)。我现在想把它们加载到我的Sitecore MVC模型中。

列表位于 sitecore>内容>解释。例如,在 Lists 文件夹中,有一个名为 Country 的文件夹。我想取回 Country 文件夹中的所有项目,并在我的视图中将它们填充为无序列表。

更新:我实现了下面建议的 Glass.Mapper.Sc 方法。它现在已全面运作。

这就是我现在的工作模式:

using Glass.Mapper.Sc.Configuration;
using Glass.Mapper.Sc.Configuration.Attributes;
using Sitecore.Data.Items;
using Sitecore.Mvc.Presentation;
using System;
using System.Collections.Generic;

namespace Sitecore.Web.Models
{
    public class Registration: IRenderingModel
    {
        public Rendering Rendering { get; set; }
        public Item Item { get; set; }
        public Item PageItem { get; set; }

        public IEnumerable<CountryChildItem> CountryList { get; set; }

        [SitecoreType(AutoMap = true)]
        public class CountryItem
        {
            public virtual IEnumerable<CountryChildItem> Children { get; set; }
        }

        [SitecoreType(AutoMap = true)]
        public class CountryChildItem
        {
            [SitecoreId]
            public virtual Guid Id { get; set; }

            [SitecoreInfo(SitecoreInfoType.Path)]
            public virtual string Path { get; set; }

            [SitecoreField]
            public virtual string DisplayName { get; set; }

            [SitecoreField]
            public virtual string Abbreviation { get; set; }
        }

        public void Initialize(Rendering rendering)
        {
            Rendering = rendering;
            Item = rendering.Item;
            PageItem = PageContext.Current.Item;
        }
    }
}

这就是我的工作控制器的样子:

using Glass.Mapper.Sc;
using Sitecore.Web.Models;
using System.Web.Mvc;

namespace Sitecore.Web.Controllers
{
    public class RegistrationController : Controller
    {
        Registration registrationModel = new Registration();

        public ActionResult Index()
        {
            ISitecoreContext sitecoreContext = new SitecoreContext();
            ISitecoreService service = new SitecoreService(sitecoreContext.Database);
            Registration.CountryItem countryItem = service.GetItem<Registration.CountryItem>("/sitecore/content/Lists/Country");
            registrationModel.CountryList = countryItem.Children;
            return View(registrationModel);
        }
    }
}

以及我工作视图的片段:

 <ul class="select-menu-options dropdown-menu">
    @foreach (var country in Model.CountryList)
    {
        <li><a href="#">@country.DisplayName</a></li>
    }
 </ul>

2 个答案:

答案 0 :(得分:2)

如果我在你的位置,我会查看Sitecore的Glassmapper。

这是Sitecore相当轻量级的ORM。

http://www.glass.lu/Mapper/Sc

我还建议移动位于

的列表
sitecore > Templates > User Defined > Lists > Content 

下的某些地方
sitecore > Content 

sitecore > System 

(无论哪个更有意义)

更新: 尝试在课程上方添加以下内容:

[SitecoreType(AutoMap =  true)]
public class CountryItem
{
    //...
}

答案 1 :(得分:0)

如果您将{% capture endofweek %} {{ forloop.index | modulo: plan.DateTypeChoice }}{% endcapture %} <p>"endofweek: " {{endofweek}}</p> //again 5 :-( {%if endofweek == 0 %} 和其他模型类更改为从CountryItem继承,则:

SearchResultItem

您应该能够使用Sitecore索引来检索所有国家/地区以及其他类似的列表:

[PredefinedQuery("TemplateID", ComparisonType.Equal, "{ID-OF-CountryItem-TEMPLATE}", typeof(ID))]
public class CountryItem : Sitecore.ContentSearch.SearchTypes.SearchResultItem
{
    [IndexField("_displayname")]
    public virtual string DisplayName { get; set; }

    [IndexField("abbreviation")]
    public string Abbreviation { get; set; }
}

请注意,这只是一个例子。您需要对其进行测试,并且最有可能适应您的解决方案。

正如Dar Brett所说,你不应该在private static string IndexName { get { return string.Format("sitecore_{0}_index", (Context.ContentDatabase ?? Context.Database).Name); } } private static string Language { get { return Context.Language.Name; } } public IEnumerable<CountryItem> GetCountries() { using (var context = ContentSearchManager.GetIndex(IndexName).CreateSearchContext()) { IQueryable<CountryItem> queryable = context.GetQueryable<CountryItem>(); queryable = queryable.Where(i => i.Language == Language); queryable = queryable.Where(i => i.LatestVersion); // ... maybe excluding standard values or some other filters var searchResults = queryable.GetResults(); return queryable.ToList(); } } 节点下保留任何数据项。