title: HTML.Textarea Values in MVC Razor View
body: <p>It is hard for me to clearly state the problem I am having.  I am trying to understand how to retain values in form fields created in a loop after validation fails.  I have a more complicated real world form that has a bunch of elements created in the loop and validation.  I have reduced it to a simple example included below.<br/>
  <br/>
  When validation fails I would like the textareas named "Comment" that have been created in the loop to retain the values that are shown in the Pre-Submit image below.<br/>
  <br/>
  When I debug the form submission, the values from each of the fields are successfully connected to the IList variable named Comment found in the Model. This is what I want so I can loop through and locate them based on index.<br/>
  <br/>
  After submitting, each textarea produced by the loop shows the comma separated representation of the IList variable Comment in the Model.  It appears that the field in the view and in the model are connecting because they share a name.  They connect properly on the way in but not on the way out.  I would like the view to only show the value associated with the Comment[i] instead of the entire list so that the values remain constant between form submissions.<br/><br/>
  <strong>Screenshots and Sample Code Below</strong>
  <br/>
  <strong>First Load:</strong><br/>
  <img src="https://i.stack.imgur.com/EnkDy.jpg" alt="First load of form without changes"><br/>
  <br/>
  <strong>Pre-Submit Form Changes:</strong><br/>
  <img src="https://i.stack.imgur.com/jjhJD.jpg" alt="Form with changes to the first input before submitting"><br/>
  <br/>
  <strong>Form as seen after first submit:</strong><br/>
  <img src="https://i.stack.imgur.com/Tw9bT.jpg" alt="Form as seen after the first submission"><br/>
  <br/>
  <strong>Form as seen after second submit:</strong><br/>
  <img src="https://i.stack.imgur.com/OKn6R.jpg" alt="enter image description here">
  <br/><br/>
  <strong>Model Code</strong><br/></p>
  
  <pre><code>using System.Collections.Generic;
  namespace UI.Models.Forms
  {
      public class TempListModel : ContentModel
      {
          public TempListModel()
          {
              Comment = new List&lt;string&gt;();
          }
          public IList&lt;string&gt; Comment { get; set; }  //Comments for each URL in the list
      }
  }
  </code></pre>
  
  <p><br/>
  <strong>View Code</strong></p>
  
  <pre><code>@model UI.Models.Forms.TempListModel  
  @using (Html.BeginForm("temptest", "Test", new { id = 1 }, FormMethod.Post, new { id = "listForm", name = "listForm" }))
  {
      &lt;ul&gt;
          @for (int i = 0; i &lt; Model.Comment.Count(); i++)
          {
              &lt;li&gt;
                  &lt;div class="llformlabel"&gt;
                      Notes:
                      &lt;div&gt;@Model.Comment[i]&lt;/div&gt;
                      @Html.TextArea("Comment", Model.Comment[i], 4, 63, new { @id = "Comment_" + i, @title = "Comment" })&lt;/div&gt;
              &lt;/li&gt;
          }
      &lt;/ul&gt;
      &lt;input type="submit" value="Save Changes" /&gt;
  }
  </code></pre>
  
  <p><br/>
  <strong>Controller Code</strong><br/></p>
  
  <pre><code>using System.Collections.Generic;
  using System.Web.Mvc;
  using UI.Models.Forms;
  namespace UI.Controllers
  {
      public class TestController : Controller
      {
          [AcceptVerbs(HttpVerbs.Post)]
          public ActionResult TempTest(TempListModel model)
          {
              //This function executes after the user submits the form.
              //If server side validation fails then the user should be shown the form as it was when they submitted.
              //model.Comment = GetComments();  //In my real world example this comes from a database.
              if (true) //!ModelState.IsValid) //In my real world code this is a validation step that may fail
              {
                  return View(model);
              }
          }
          [AcceptVerbs(HttpVerbs.Get)]
          public ActionResult TempTest(int? id)
          {
              //In the real world example there is a lot going on in this function.
              //It is used to load data from databases and set up the model to be displayed.
              var model = new TempListModel(); 
              model.Comment = GetComments();
              return View("TempTest", "TempLayout", model);
          }
          private static IList&lt;string&gt; GetComments()
          {
              //Simple sample function used for demo purposes.
              IList&lt;string&gt; comments = new List&lt;string&gt;();
              comments.Add("Comment 1");
              comments.Add("Comment 2");
              comments.Add("Comment 3");
              return comments;
          }
      }
  }
  </code></pre>