I am trying to show a table that contains a list of all items in a bucket. I've created the following ActionResult:
public ActionResult Outage()
{
if (IsDataSourceItemNull) return null;
IEnumerable<SimpleItem> items = DataSourceItems.Select(x => new SimpleItem(x)).Where(x => SiteConfiguration.DoesItemExistInCurrentLanguage(x.Item));
SimpleItemList results = new SimpleItemList(DataSourceItem["Title"], items);
return !items.IsNullOrEmpty() ? View(results) : ShowListIsEmptyPageEditorAlert();
}
The code-behind for the controller rendering is this:
@model LaunchSitecore.Models.SimpleItemList
<div>
<table class="table table-striped">
<thead>
<tr>
<th>Title</th>
<th>Date</th>
<th>Description</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Items)
{
<tr>
<td>@Html.Sitecore().Field("Title")</td>
<td>@Html.Sitecore().Field("Date")</td>
<td>@Html.Sitecore().Field("Description")</td>
</tr>
}
</tbody>
</table>
</div>
And the model is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sitecore.Mvc;
using Sitecore.Mvc.Presentation;
using System.Xml.Serialization;
using Sitecore.Data.Items;
using Sitecore.Links;
using LaunchSitecore.Configuration;
namespace LaunchSitecore.Models
{
public class SimpleItemList
{
public string Title { get; protected set; }
public IEnumerable<SimpleItem> Items { get; protected set; }
public SimpleItemList(string title, IEnumerable<SimpleItem> items)
{
Title = title;
Items = items;
}
}
}
I am using this query as the data source in my rendering:
Finally, here is what I see on my page:
I'm not sure why the title of my query is being returned...It is being generated the proper amount of times for how many "outage" items I have, but this obviously is not the desired output. I should see the data from the actual "outage" content items displayed in the table. Can anyone diagnose my problem?
It seems if you don't supply the item as well as the field name, then it will look for that field on the context item, which is your query, I guess. So you can try supplying the item
as well, like so:
@foreach (var item in Model.Items)
{
<tr>
<td>@Html.Sitecore().Field("Title", item)</td>
<td>@Html.Sitecore().Field("Date", item)</td>
<td>@Html.Sitecore().Field("Description", item)</td>
</tr>
}
More info here: Rendering Content.