我已经创建了一堆自定义模板来在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内容树的设置。 –
添加了项目在树中位置的更新。至于“你所尝试的清单”,这就是我发布这个问题的原因。 –