title: ASP.Net MVC Array of Hidden fields
body: <p>I am sending a List&lt;> to a View, and then turning that list into an array of hidden fields. This is being done using a partial view, to build the table.</p>
  
  <pre><code>&lt;%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl&lt;BudgieMoneySite.Models.TransactionSplitLine&gt;" %&gt;
  &lt;tr&gt;
      &lt;td&gt;
          &lt;%=Model.Category %&gt;
          &lt;%=Html.HiddenFor(x =&gt; x.CategoryId)%&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;%=Model.SubCategory %&gt;
          &lt;%=Html.HiddenFor(x =&gt; x.SubCategoryId)%&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;%=Model.Amount %&gt;
          &lt;%=Html.HiddenFor(x =&gt; x.AmountValue)%&gt;
      &lt;/td&gt;
  
  &lt;/tr&gt;
  </code></pre>
  
  <p>I have have x number of rows.... so I am hoping I can get the values from the hidden fields as an array. I have a field in my model, defined as public string[] CategoryIds { get; set; }</p>
  
  <p>(I think the issue may be around the hidden field being called 'CategoryId' and mu model wanting 'CategoryIds').</p>
  
  <p>When rendered, I see I have a row which looks like this:</p>
  
  <pre><code>&lt;tr&gt;
  &lt;td&gt;
      Medical
      &lt;input id="TransactionSplitLines_2__CategoryId" name="TransactionSplitLines[2].CategoryId" type="hidden" value="6" /&gt;
  
  &lt;/td&gt;
  &lt;td&gt;
      Over the Counter Medicines
      &lt;input id="TransactionSplitLines_2__SubCategoryId" name="TransactionSplitLines[2].SubCategoryId" type="hidden" value="22" /&gt;
  &lt;/td&gt;
  &lt;td&gt;
      111
      &lt;input id="TransactionSplitLines_2__AmountValue" name="TransactionSplitLines[2].AmountValue" type="hidden" value="0" /&gt;
  &lt;/td&gt;
  </code></pre>
  
  <p></p>
  
  <p>I am then trying to get the data back in the controller like this:</p>
  
  <pre><code>[HttpPost]
  public ActionResult AccountTransaction(AccountTransactionView model)
  {
      var reply = CreateModel(model);
      if (model.CategoryIds != null)
      {
          for (int i = 0; i &lt; model.CategoryIds.Count(); i++ )
          {
              reply.TransactionSplitLines.Add(new TransactionSplitLine
                                                  {
                                                      Amount = "100", 
                                                      Category = Services.CategoryServices.GetCategoryById(int.Parse(model.CategoryIds[i])).Description, 
                                                      SubCategoryId = model.SelectedCategoryId, 
                                                      SubCategory = Services.SubCategoryServices.GetSubCategoryById(model.SelectedSubCategoryId).Description
                                                  });
          }
      }
      reply.TransactionSplitLines.Add(new TransactionSplitLine
                                          {
                                              Amount = model.Amount, 
                                              Category = Services.CategoryServices.GetCategoryById(model.SelectedCategoryId).Description, 
                                              SubCategory = Services.SubCategoryServices.GetSubCategoryById(model.SelectedSubCategoryId).Description, 
                                              CategoryId = model.SelectedCategoryId,
                                              SubCategoryId = model.SelectedSubCategoryId
                                          });
      return View("AccountTransaction", reply);
  }
  </code></pre>
  
  <p>But CategoryIds is always null...</p>
  
  <p>How can I reference the hidden fields, when their name is 'TransactionSplitLines[2].SubCategoryId' (Which is the 2nd row, I think). I think I have a naming issue, and am just getting tangled... but hopefully you can assit?</p>
  
  <p>EDIT: Here's the AccountTransactionView class..</p>
  
  <pre><code> public class AccountTransactionView
  {
    public AccountTransactionView()
    {
        TransactionSplitLines = new List&lt;TransactionSplitLine&gt;();
    }
  
      [DisplayName("Bank Account")]
      public IEnumerable&lt;SelectListItem&gt; Accounts { get; set; }
  
      [DisplayName("Cost Center")]
      public IEnumerable&lt;SelectListItem&gt; CostCenters { get; set; }
  
      [DisplayName("Payee")]
      public IEnumerable&lt;SelectListItem&gt; Payees { get; set; }
  
      [DisplayName("Transaction Type")]
      public List&lt;TransactionTypeView&gt; TransactionTypes { get; set; }
  
      public int SelectedAccountId { get; set; }
      public int SelectedCostCenterId { get; set; }
      public int SelectedPayeeId { get; set; }
      [DisplayName("Category")]
      public int SelectedCategoryId { get; set; }
      [DisplayName("Sub Category")]
      public int SelectedSubCategoryId { get; set; }
      public int SelectedTransactionTypeId { get; set; }
      [DisplayName("Budget")]
      public int SelectedBudgetId { get; set; }
  
      [DisplayName("Transaction Date")]
      public DateTime TransactionDate { get; set; }
      [DisplayName("Transaction Date")]
      public string FormattedDateTime
      {
          get
          {
              return TransactionDate.ToString("dd/MM/yyyy");
  
          }
      }
      public int TransactionId { get; set; }
      public bool Deleted { get; set; }
      public List&lt;TransactionSplitLine&gt; TransactionSplitLines { get; set; }
      public string Amount { get; set; }
  
  }
  </code></pre>