Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As BuildStarted suggested, I used Phil Haack's post to formulate a solution. </p> <pre><code>public static HelperResult Each&lt;TItem&gt;(this IEnumerable&lt;TItem&gt; items, Func&lt;EnumeratedItem&lt;TItem&gt;, HelperResult&gt; template) { return new HelperResult(writer =&gt; { int index = 0; ICollection&lt;TItem&gt; list = items.ToList(); foreach (var item in list) { var result = template(new EnumeratedItem&lt;TItem&gt;(index, index == 0, index == list.Count - 1, item)); index++; result.WriteTo(writer); } }); } </code></pre> <p>And the EnumeratedItem class:</p> <pre><code>public class EnumeratedItem&lt;TModel&gt; { public EnumeratedItem(int index, bool isFirst, bool isLast, TModel item) { this.Index = index; this.IsFirst = isFirst; this.IsLast = isLast; this.Item = item; } public int Index { get; private set; } public bool IsFirst { get; private set; } public bool IsLast { get; private set; } public TModel Item { get; private set; } } </code></pre> <p>I was forced to convert the IEnumerable to an ICollection in order to have the Count property.</p> <p>Usage:</p> <pre><code>@Model.History.Each( @&lt;text&gt; &lt;li class="@(@item.IsLast ? "last" : string.Empty)"&gt; @{ var versionNo = Model.History.Count - @item.Index; } &lt;div class="clearfix version selected"&gt; &lt;div class="index"&gt; &lt;a href="@item.Item.Url"&gt;@versionNo&lt;/a&gt; &lt;/div&gt; &lt;div class="modified"&gt; @item.Item.DateModified&lt;br/&gt; @Html.Raw(item.Item.ModifiedBy) &lt;/div&gt; &lt;div class="view"&gt; &lt;a href="@item.Item.CompareUrl"&gt;View changes&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/li&gt; &lt;/text&gt;) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload