I have a couple follow-up questions for this question: Sitecore MVC - Accessing the Data Source of a Control
In the foreach loop, how do I output an image?
Since the model for the view is now strongly typed, how can I get the RenderingModel fields using the Html helper syntax?
@Html.Sitecore().Field("Title")
In my controller I have:
public ActionResult Index()
{
var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
var dataSource = Sitecore.Context.Database.GetItem(dataSourceId);
var viewModel = new RotatorViewModel();
viewModel.RotatorItems = dataSource.Children;
viewModel.Initialize(RenderingContext.Current.Rendering);
return View(viewModel);
}
I am trying to inherit RenderingModel:
public class RotatorViewModel : RenderingModel
{
public IEnumerable<Item> RotatorItems { get; set; }
public HtmlString Title
{
get
{
return new HtmlString(FieldRenderer.Render(this.Item, "Title"));
}
}
public HtmlString Text
{
get
{
return new HtmlString(FieldRenderer.Render(this.Item, "Text"));
}
}
}
In my view I have:
@using Sitecore.Mvc.Presentation
@using Sitecore.Mvc
@model BentleyBootstrap.Controllers.HomeController.RotatorViewModel
@foreach (var item in Model.RotatorItems)
{
<h2>@item.Fields["Title"]</h2>
@item.Fields["Sub Title"]
}
@Model.Title
<br />
@Model.Text