title: How to produce non-sequential prefix collection indices with MVC HTML Editor templates?
body: <p>The following code has been stripped down a lot, but basically what I'm looking to achieve is as follows:</p>
  
  <p>I'd like to able to edit Questions and their containing Answer Choices, while being able to dynamically add/remove Questions/Answer Choices from the page. Ideally, the HtmlFieldPrefix for my items would be non-sequential, but Html.EditorFor() uses a sequential index.</p>
  
  <p>I have a Question ViewModel that contains an IEnumerable of Answer Choices:</p>
  
  <pre><code>public class QuestionViewModel
  {
      public int QuestionId { get; set; }
      public IEnumerable&lt;AnswerChoiceViewModel&gt; AnswerChoices { get; set; }
  }
  </code></pre>
  
  <p>In my Question partial view (Question.ascx), I have this:</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Models.QuestionViewModel&gt;" %&gt;
  
  &lt;%=Html.HiddenFor(m =&gt; m.QuestionId)%&gt;
  &lt;%=Html.EditorFor(m =&gt; m.AnswerChoices) %&gt;
  </code></pre>
  
  <p>And the Answer Choice editor template (AnswerChoiceViewModel.ascx):</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;Models.AnswerChoiceViewModel&gt;" %&gt;
  
  &lt;%=Html.HiddenFor(m =&gt; m.AnswerChoiceId)%&gt;
  &lt;%=Html.TextBoxFor(m =&gt; m.Name)%&gt;
  </code></pre>
  
  <p>When I render Question.ascx, the output will look as follows:</p>
  
  <pre><code>&lt;input type="hidden" id="QuestionId" value="1" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[0].AnswerChoiceId" value="1" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[0].Name" value="Answer Choice 1" /&gt;
  
  &lt;input type="hidden" id="QuestionId" value="2" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[1].AnswerChoiceId" value="2" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[1].Name" value="Answer Choice 2" /&gt;
  </code></pre>
  
  <p>What I want to know is how I can provide EditorFor a custom GUID index so that the page would render like this:</p>
  
  <pre><code>&lt;input type="hidden" id="QuestionId" value="1" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[e1424d5e-5585-413c-a1b0-595f39747876].AnswerChoiceId" value="1" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[e1424d5e-5585-413c-a1b0-595f39747876].Name" value="Answer Choice 1" /&gt;
  
  &lt;input type="hidden" id="QuestionId" value="2" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[633db1c3-f1e6-470b-9c7f-c138f2d9fa71].AnswerChoiceId" value="2" /&gt;
  &lt;input type="hidden" id="Question.AnswerChoices[633db1c3-f1e6-470b-9c7f-c138f2d9fa71].Name" value="Answer Choice 2" /&gt;
  </code></pre>
  
  <p>I have already written a helper method that will get the prefix index of the current context and store it in a hidden ".Index" field so that non-sequential indices can be bound correctly. Just want to know how EditorFor is assigning the indexes so that I can override it (or any other working solution).</p>