2015-08-31 21 views
1

我已经创建了一堆自定义模板来在Sitecore中存储项目(例如Industries,Subindustries等)。我现在想要将这些加载到我的Sitecore MVC模型中。Sitecore将所有项目加载到MVC模型中?


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


UPDATE:我实现了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> 
+0

这个问题是非常通用的 - 也许列出你已经尝试过什么东西来获取数据?很多将取决于您的Sitecore内容树的设置。 –

+0

添加了项目在树中位置的更新。至于“你所尝试的清单”,这就是我发布这个问题的原因。 –

回答

2

如果我处于您的位置,我会寻找Sitemap的Glassmapper。

这是一个相当轻量级的Sitecore ORM。

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

我也建议下地方移动位于

sitecore > Templates > User Defined > Lists > Content 

一些名单要么

sitecore > Content 

sitecore > System 

(whiche版本更有SENCE)

更新:如果您改变CountryItem和其他模型类从SearchResultItem继承这样的

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

[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; } 
} 

您 尝试加入这个类以上应该可以使用Sitecore索引来检索所有国家和其他列表:

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(); 
    } 
} 

请注意,这只是一个例子。你需要测试它,并且很可能适应你的解决方案。

正如Dar Brett所述,您不应该在Templates节点下保留任何数据项。

+0

将内容项目移出并更新了上述问题。 –

相关问题