Skip to content

Instantly share code, notes, and snippets.

@chilversc
Last active December 16, 2015 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chilversc/5371667 to your computer and use it in GitHub Desktop.
Save chilversc/5371667 to your computer and use it in GitHub Desktop.
Simple indexed prefix for razor foreach loop
@foreach (var item in Html.Prefix(Model.Results, "result")) {
<label for="@Html.Id("comment")>Comment</label>
@Html.TextArea("comment", item.Comment)
}
@using (Html.Prefix("test")) {
<label for="@Html.Id("nested")">Nested</label>
@Html.TextArea("nested")
}
<label for="@Html.Id("notes")>Notes</label>
@Html.TextArea("notes", Model.Notes)
@* Output:
<label for="result_0__comment">Comment</label>
<textarea id="result_0__comment" name="result[0].comment"></textarea>
<label for="result_1__comment">Comment</label>
<textarea id="result_1__comment" name="result[1].comment"></textarea>
<label for="result_2__comment">Comment</label>
<textarea id="result_2__comment" name="result[2].comment"></textarea>
<label for="test_nested">Nested</label>
<textarea id="test_nested" name="test.nested"></textarea>
<label for="notes">Notes</label>
<textarea id="notes" name="notes"></textarea>
*@
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public static class PrefixExtensions
{
public static HtmlNamePrefixRegion Prefix (this HtmlHelper html, string prefix)
{
var templateInfo = html.ViewData.TemplateInfo;
var fullPrefix = templateInfo.GetFullHtmlFieldName (prefix);
return new HtmlNamePrefixRegion (templateInfo, fullPrefix);
}
public static HtmlIndexedNamePrefixRegion Prefix (this HtmlHelper html, string prefix, int index)
{
var templateInfo = html.ViewData.TemplateInfo;
return new HtmlIndexedNamePrefixRegion (templateInfo, prefix, index);
}
public static IEnumerable<T> Prefix<T> (this HtmlHelper html, IEnumerable<T> items, string prefix)
{
return html.Prefix (items, prefix, 0);
}
public static IEnumerable<T> Prefix<T> (this HtmlHelper html, IEnumerable<T> items, string prefix, int initialIndex)
{
using (var counter = html.Prefix (prefix, initialIndex)) {
foreach (var item in items) {
yield return item;
counter.Next ();
}
}
}
}
public class HtmlIndexedNamePrefixRegion : IDisposable
{
private readonly TemplateInfo template;
private readonly string originalPrefix;
private readonly string partialPrefix;
private int index;
public HtmlIndexedNamePrefixRegion (TemplateInfo template, string partialPrefix, int initialIndex)
{
this.template = template;
this.partialPrefix = partialPrefix;
index = initialIndex;
originalPrefix = template.HtmlFieldPrefix;
Update ();
}
public void Next ()
{
index++;
Update ();
}
private void Update ()
{
template.HtmlFieldPrefix = originalPrefix;
var fullPrefix = template.GetFullHtmlFieldName (string.Format ("{0}[{1}]", partialPrefix, index));
template.HtmlFieldPrefix = fullPrefix;
}
public void Dispose ()
{
template.HtmlFieldPrefix = originalPrefix;
}
}
public struct HtmlNamePrefixRegion : IDisposable
{
private readonly TemplateInfo template;
private readonly string originalPrefix;
public HtmlNamePrefixRegion (TemplateInfo template, string newPrefix)
{
this.template = template;
originalPrefix = template.HtmlFieldPrefix;
template.HtmlFieldPrefix = newPrefix;
}
public void Dispose ()
{
template.HtmlFieldPrefix = originalPrefix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment