How do I bind a Custom Item generated with "Custom Item Generator" to my view model for a controller rendering? There is a data source that is returning a list of the Custom Items. Also, one of the custom items is an image. It sounds like I should be using Glass Mapper, but it's not clear to me what it does and how that relates to what I am trying to do. This code does not build.
View:
@using Sitecore.Mvc.Presentation
@using Sitecore.Mvc
@model MyCompanyBootstrap.CustomItems.RotatorContentItem
@foreach (var item in Model.RotatorItems)
{
<h2>@item.Fields["Title"]</h2>
@item.Fields["Sub Title"]
}
@Model.Title
<br />
@Model.Text
Controller Action:
public ActionResult Index()
{
var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
var dataSource = Sitecore.Context.Database.GetItem(dataSourceId);
var viewModel = new RotatorContentItem();
viewModel.RotatorItems = dataSource.Children;
viewModel.Initialize(RenderingContext.Current.Rendering);
return View(viewModel);
}
Custom Item:
public partial class RotatorContentItem : CustomItem
{
public static readonly string TemplateId = "{E493DF4A-E97E-49B5-BF9F-F70397EA8D21}";
#region Boilerplate CustomItem Code
public RotatorContentItem(Item innerItem) : base(innerItem)
{
}
public static implicit operator RotatorContentItem(Item innerItem)
{
return innerItem != null ? new RotatorContentItem(innerItem) : null;
}
public static implicit operator Item(RotatorContentItem customItem)
{
return customItem != null ? customItem.InnerItem : null;
}
#endregion //Boilerplate CustomItem Code
#region Field Instance Methods
public CustomTextField Title
{
get
{
return new CustomTextField(InnerItem, InnerItem.Fields["Title"]);
}
}
public CustomTextField SubTitle
{
get
{
return new CustomTextField(InnerItem, InnerItem.Fields["Sub Title"]);
}
}
public CustomImageField ImageLarge
{
get
{
return new CustomImageField(InnerItem, InnerItem.Fields["Image Large"]);
}
}
public CustomImageField ImageSmall
{
get
{
return new CustomImageField(InnerItem, InnerItem.Fields["Image Small"]);
}
}
public CustomGeneralLinkField Link
{
get
{
return new CustomGeneralLinkField(InnerItem, InnerItem.Fields["Link"]);
}
}
public CustomCheckboxField IsOverlayBlack
{
get
{
return new CustomCheckboxField(InnerItem, InnerItem.Fields["Is Overlay Black"]);
}
}
#endregion //Field Instance Methods
}
Edit: There are a number of things wrong with this code that I am seeing now. I think there should be a new class specifically for the View Model itself. And that class should have a list of RotatorContentItems.
public List<RotatorContentItem> RotatorContentItems { get; set; }
But, it is not clear to me how to bind that list in the controller action.
public class MyViewModel
{
public List<RotatorContentItem> RotatorContentItems { get; set; }
public IEnumerable<Item> RotatorItems { get; set; }
}
public ActionResult Index()
{
var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
var dataSource = Sitecore.Context.Database.GetItem(dataSourceId);
var viewModel = new MyViewModel();
viewModel.RotatorContentItems = dataSource.Children;
viewModel.RotatorItems = dataSource.Children;
return View(viewModel);
}
This will not build on the following line:
viewModel.RotatorContentItems = dataSource.Children;
Error is:
Cannot implicitly convert type 'Sitecore.Collections.ChildList' to 'System.Collections.Generic.List<MyCompnayBootstrap.CustomItems.RotatorContentItem>'