Я создал несколько пользовательских шаблонов для хранения элементов (таких как Industries
, Subindustries
и т. д.) в Sitecore. Теперь я хочу загрузить их в мою модель Sitecore MVC.
Списки находятся в разделе sitecore > Content > Lists. Например, внутри папки Списки есть папка с названием Страна. Я хочу вернуть все элементы внутри папки 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>