我已經創建了一堆自定義模板來在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>
這個問題是非常通用的 - 也許列出你已經嘗試過什麼東西來獲取數據?很多將取決於您的Sitecore內容樹的設置。 –
添加了項目在樹中位置的更新。至於「你所嘗試的清單」,這就是我發佈這個問題的原因。 –