title: MVC3 Model Binding - List to Hidden fields
body: <p>I have a peculiar problem - I have a ViewModel with a List in it used to display a list of images:</p>
  
  <pre><code>public List&lt;int&gt; TrackerKeys { get; set; }
  </code></pre>
  
  <p>This is used in two places in the page:</p>
  
  <pre><code>@for (int i = 0; i &lt; Model.TrackerKeys.Count(); i++)
  { 
      @Html.GenerateImage(PageModes.Http, Model.TrackerKeys[i])
  }
  </code></pre>
  
  <p>And also</p>
  
  <pre><code>@for (int i = 0; i &lt; Model.TrackerKeys.Count(); i++)
  { 
      @Html.HiddenFor(model =&gt; model.TrackerKeys[i])
  }
  </code></pre>
  
  <p>This is sat inside a form - when the form is submitted, if a validation error occurs, the TrackerKeys property is updated with a new random set of numbers. I am passing the previous list back to ensure the user does not see the same image again.</p>
  
  <p>Tracker keys is correctly set and passed back to the View. </p>
  
  <p><strong>My problem is:</strong></p>
  
  <p>The images correctly display the new images based on the new values in the list</p>
  
  <p><strong>However</strong></p>
  
  <p>The values of the hidden fields are not updated to the new values - they retain the original values.</p>
  
  <p>Any ideas? Is this a bug with MVC3? any input would be greatly appreciated. Thank you.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>Html before submission:</p>
  
  <pre><code>&lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/26.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/33.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/8.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/30.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/6.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/18.gif" width="35" /&gt;  
  </code></pre>
  
  <p>And</p>
  
  <pre><code>&lt;input id="TrackerKeys_0_" name="TrackerKeys[0]" type="hidden" value="26" /&gt;
  &lt;input id="TrackerKeys_1_" name="TrackerKeys[1]" type="hidden" value="33" /&gt;
  &lt;input id="TrackerKeys_2_" name="TrackerKeys[2]" type="hidden" value="8" /&gt;
  &lt;input id="TrackerKeys_3_" name="TrackerKeys[3]" type="hidden" value="30" /&gt;
  &lt;input id="TrackerKeys_4_" name="TrackerKeys[4]" type="hidden" value="6" /&gt;
  &lt;input id="TrackerKeys_5_" name="TrackerKeys[5]" type="hidden" value="18" /&gt;
  </code></pre>
  
  <p>And after the post:
  correct new values here...</p>
  
  <pre><code>&lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/16.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/20.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/11.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/19.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/26.gif" width="35" /&gt;
  &lt;img alt="Security Character" height="35" src="http://localhost:51414/content/imagescss/15.gif" width="35" /&gt; 
  </code></pre>
  
  <p>and...
  still retaining old values...</p>
  
  <pre><code>&lt;input id="TrackerKeys_0_" name="TrackerKeys[0]" type="hidden" value="26" /&gt;
  &lt;input id="TrackerKeys_1_" name="TrackerKeys[1]" type="hidden" value="33" /&gt;
  &lt;input id="TrackerKeys_2_" name="TrackerKeys[2]" type="hidden" value="8" /&gt;
  &lt;input id="TrackerKeys_3_" name="TrackerKeys[3]" type="hidden" value="30" /&gt;
  &lt;input id="TrackerKeys_4_" name="TrackerKeys[4]" type="hidden" value="6" /&gt;
  &lt;input id="TrackerKeys_5_" name="TrackerKeys[5]" type="hidden" value="18" /&gt;
  </code></pre>
  
  <p><strong>controller action</strong></p>
  
  <pre><code>[HttpPost]
      public ActionResult EmployerRegistration(EmployerViewModel Model)
      {
          if (ModelState.IsValid)
          {
              //do bits here
          }
  
          Model.TrackerKeys = Helper.GenerateNewNumbers(Model.TrackerKeys); 
          return View(Model);
      }
  </code></pre>