title: MVC Textbox binding multiple properties
body: <p>I am trying to figure out the best way to present/save the data in a view I am creating.</p>
  <p>I am not wedded to the underlying table structure, and have played around with it trying to get the best outcome but no luck.</p>
  <p>Effectively I have multiple textbox's that need to be saved back to the database, but due to the view layout (and it really needs to stay in this layout), I need to populate additional columns with hard coded info.</p>
  <p>In my view I have just repeated the textbox ( I tried to index it, but got an error saying it wasn't possible on decimal data types?)</p>
  <p>Below is my model</p>
  <pre><code>    namespace Testing.Models
  {
      public partial class OnFarm
      {
          public int Id { get; set; }
          public Guid? Tracker { get; set; }
          public int Year { get; set; }
          public decimal? Period1 { get; set; }
          public decimal? Period2 { get; set; }
          public decimal? Period3 { get; set; }
          public decimal? Period4 { get; set; }
          public decimal? Period5 { get; set; }
          public decimal? Period6 { get; set; }
          public decimal? Period7 { get; set; }
          public decimal? Period8 { get; set; }
          public decimal? Period9 { get; set; }
          public decimal? Period10 { get; set; }
          public decimal? Period11 { get; set; }
          public decimal? Period12 { get; set; }
          public int Version { get; set; }
          public int Account { get; set; }
          public int MvtType { get; set; }
  
          public virtual Account AccountNavigation { get; set; }
          public virtual MovementType MvtTypeNavigation { get; set; }
          public virtual ProductionModule TrackerNavigation { get; set; }
          public virtual Version VersionNavigation { get; set; }
          public virtual Years YearNavigation { get; set; }
      }
  }
  </code></pre>
  <p>This is my controller as it stands currently</p>
  <pre><code>        public IActionResult Create(Guid? id)
          {
              var query = _context.LegalEntities.FromSqlRaw(&quot;Select DISTINCT B.* From Client.Groups a Left join Client.[Legal Entities] b on b.Parent = a.ID left join client.Enterprises c on c.Parent = b.ID left join client.[Production Module] d on d.Parent = c.ID   left join OnFarm.[On Farm] e on e.Tracker = d.ID Where e.id IS NOT NULL AND a.id = {0}&quot;, id).ToList();
  
              List&lt;SelectListItem&gt; LEList = new List&lt;SelectListItem&gt;();
  
              foreach (var m in query)
              {
                  LEList.Add(new SelectListItem { Text = m.Name, Value = m.Id.ToString() });
              }
              ViewBag.LE = LEList;
  
              var acclist = _context.Accounts.Where(x =&gt; x.AccountCatId == 92100).ToList().OrderBy(x =&gt; x.Description);
              List&lt;SelectListItem&gt; listac = new List&lt;SelectListItem&gt;();
  
              listac.Add(new SelectListItem { Text = &quot;--Select Livestock Type--&quot;, Value = &quot;0&quot; });
              if (acclist != null)
              {
                  var i = 1;
                  foreach (var x in acclist)
                  {
                      listac.Add(new SelectListItem { Text = x.Description, Value = x.Number.ToString() });
                      i += 1;
                  }
              }
              ViewBag.Account = listac;
  
  
              ViewData[&quot;MvtType&quot;] = new SelectList(_context.MovementTypes, &quot;Id&quot;, &quot;Type&quot;);
              ViewData[&quot;Tracker&quot;] = new SelectList(_context.ProductionModules, &quot;Id&quot;, &quot;Name&quot;);
              ViewData[&quot;Version&quot;] = new SelectList(_context.Versions, &quot;VersionId&quot;, &quot;VersionName&quot;);
              ViewData[&quot;Year&quot;] = new SelectList(_context.Year, &quot;Id&quot;, &quot;Year&quot;);
              return View();
          }
  </code></pre>
  <p>Here is my view</p>
  <pre><code>&lt;div class=&quot;row&quot;&gt;
      @foreach (var item in ViewBag.LE)
      {
          &lt;div class=&quot;panel panel-default rounded shadow col-11&quot;&gt;
              &lt;div class=&quot;panel-heading border-bottom&quot;&gt;
                  &lt;div class=&quot;no-gutters&quot;&gt;&lt;h4&gt;&lt;b&gt;@item.Text&lt;/b&gt;&lt;/h4&gt;&lt;/div&gt;
              &lt;/div&gt;
              &lt;div class=&quot;panel-body&quot;&gt;
                  &lt;div class=&quot;row&quot;&gt;
  
                      @foreach (var i in ViewBag.Tracker)
                      {
                          &lt;div class=&quot;row d-flex align-items-center&quot;&gt;
                              &lt;div class=&quot;panel panel-default rounded shadow col&quot;&gt;
                                  &lt;div class=&quot;panel-heading border-bottom&quot;&gt;
                                      &lt;div class=&quot;no-gutters text-muted col-5&quot;&gt;&lt;h3&gt;&lt;b&gt; @i.Text&lt;/b&gt;&lt;/h3&gt;&lt;/div&gt;
                                      &lt;div class=&quot;col-2&quot;&gt;
                                          &lt;select asp-for=&quot;Account&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.Account&quot;&gt;&lt;/select&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;div class=&quot;panel-body&quot;&gt;
                                      &lt;div class=&quot;row d-flex align-items-center&quot;&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;&lt;b&gt;Month&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Opening Balance&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Births&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Deaths&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Purchases&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Sold&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Age In&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Age Out&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Adjustments&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Closing Balance&lt;/b&gt;&lt;/div&gt;
                                      &lt;/div&gt;
                                      &lt;div class=&quot;row d-flex align-items-center&quot;&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              Apr
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot;, disabled = &quot;disabled&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot;, disabled = &quot;disabled&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
  
  
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                      }
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      }
  &lt;/div&gt;
  </code></pre>
  <p>Now the panel body is repeated 12 times (for each month of the year).</p>
  <p>When saving data back, I will have some hidden fields for some of the core info. BUT what i need to be able to do is when saving back the first text box it also needs to save the MvtType (hard coded as column headings) as a column value.</p>
  <p>I will wrap the panels into a form so it will submit when a button is clicked.</p>
  <p>Finally, the underlying table. I used to have a column called period, one called value, as thought that might have been a better way of doing it? Rather than each period separated out.</p>
  <p>Oh and sorry, I will need to load data to these textboxes when loading the page</p>
  <p>Any help is much appreciated</p>
  <p><strong>EDIT</strong>
  Ok, so i have made some progress here. I have tided up my view (See below) and can now get my view to pass back the first column of movement types, but nothing in the other columns. I effectively want to be able to post back a value into the period column while adding a value to the Mvt Type Column.</p>
  <pre><code>                                &lt;div class=&quot;row&quot;&gt;
      @foreach (var item in ViewBag.LE)
      {
      &lt;div class=&quot;panel panel-default rounded shadow col-11&quot;&gt;
          &lt;div class=&quot;panel-heading border-bottom&quot;&gt;
              &lt;div class=&quot;no-gutters&quot;&gt;&lt;h4&gt;&lt;b&gt;@item.Text&lt;/b&gt;&lt;/h4&gt;&lt;/div&gt;
          &lt;/div&gt;
          &lt;div class=&quot;panel-body&quot;&gt;
              &lt;div class=&quot;row&quot;&gt;
  
                  @foreach (var i in ViewBag.Tracker)
                  {
                      &lt;form asp-action=&quot;Create&quot; class=&quot;form-group&quot;&gt;
  
                          &lt;div class=&quot;form-group&quot;&gt;
                              &lt;label asp-for=&quot;Tracker&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                              &lt;select asp-for=&quot;Tracker&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.Tracker&quot;&gt;&lt;/select&gt;
                          &lt;/div&gt;
  
                          &lt;div class=&quot;form-group&quot;&gt;
                              &lt;label asp-for=&quot;Year&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                              &lt;select asp-for=&quot;Year&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.Year&quot;&gt;&lt;/select&gt;
                          &lt;/div&gt;
  
                          &lt;div class=&quot;form-group&quot;&gt;
                              &lt;label asp-for=&quot;Version&quot; class=&quot;control-label&quot;&gt;&lt;/label&gt;
                              &lt;select asp-for=&quot;Version&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.Version&quot;&gt;&lt;/select&gt;
                          &lt;/div&gt;
  
  
                          &lt;div class=&quot;row d-flex align-items-center&quot;&gt;
                              &lt;div class=&quot;panel panel-default rounded shadow col&quot;&gt;
                                  &lt;div class=&quot;panel-heading border-bottom&quot;&gt;
                                      &lt;div class=&quot;no-gutters text-muted col-5&quot;&gt;&lt;h3&gt;&lt;b&gt; @i.Text&lt;/b&gt;&lt;/h3&gt;&lt;/div&gt;
                                      &lt;div class=&quot;col-2&quot;&gt;
                                          &lt;select asp-for=&quot;Account&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.Account&quot;&gt;&lt;/select&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                                  &lt;div class=&quot;panel-body&quot;&gt;
                                      &lt;div class=&quot;row d-flex align-items-center&quot;&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;&lt;b&gt;Month&lt;/b&gt;&lt;/div&gt;
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Opening Balance&lt;/b&gt;&lt;/div&gt;
                                          @foreach (var mvt in ViewBag.MVtType)
                                          {
                                              &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;
                                                  &lt;b&gt;@mvt.Text&lt;/b&gt;
                                              &lt;/div&gt;
                                          }
  
                                          &lt;div class=&quot;col-1 d-flex justify-content-center&quot;&gt;&lt;b&gt;Closing Balance&lt;/b&gt;&lt;/div&gt;
                                      &lt;/div&gt;
  
                                      &lt;div class=&quot;row d-flex align-items-center&quot;&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              Apr
                                          &lt;/div&gt;
                                          &lt;div class=&quot;col-1&quot;&gt;
                                              @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot;, disabled = &quot;disabled&quot; })
                                              &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                          &lt;/div&gt;
  
                                          @for (int z = 0; z &lt; ViewBag.MvtType.Count; z++)
                                          {
  
                                              &lt;div class=&quot;col-1&quot;&gt;
                                        
                                                  &lt;select asp-for= &quot;MvtType&quot; class=&quot;form-control&quot; asp-items=&quot;ViewBag.MvtType&quot;&gt;&lt;/select&gt;
                                                  @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot; })
                                                  &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                              &lt;/div&gt;
                                          }
  
                                              &lt;div class=&quot;col-1&quot;&gt;
                                                  @Html.TextBoxFor(m =&gt; m.Period1, new { @class = &quot;form-control&quot;, disabled = &quot;disabled&quot; })
                                                  &lt;span asp-validation-for=&quot;Period1&quot; class=&quot;text-danger&quot;&gt;&lt;/span&gt;
                                              &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;row d-flex align-items-center&quot;&gt;
                              &lt;div class=&quot;col-8&quot;&gt;&lt;/div&gt;
                              &lt;div class=&quot;col-2&quot;&gt;
                                  &lt;input type=&quot;submit&quot; value=&quot;Save&quot; class=&quot;btn btn-primary float-right&quot; /&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
  
                      &lt;/form&gt;
                  }
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      }
  &lt;/div&gt;
  
  </code></pre>