2015-08-31 22 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

將內容項目移出並更新了上述問題。 –

相關問題