title: How do I update a hidden field in jquery for dynamically created rows in a table
body: <p>I am using a partial view and JQuery to dynamically add rows to a table.</p>
  
  <p>Basically very similar to <a href="https://stackoverflow.com/questions/29774822/mvc-5-begincollectionitem-with-partial-crud">MVC 5 BeginCollectionItem with Partial CRUD</a></p>
  
  <p>I have a Total column that is simply the price * qty.</p>
  
  <p>another cell is using @Html.TextBoxFor to enter a discount percent.</p>
  
  <pre><code>&lt;td&gt;
      @Html.TextBoxFor(m =&gt; m.PercentDiscount, new {@class = "form-control discount", type = "number"})
      @Html.HiddenFor(m =&gt; m.PercentDiscount)
  &lt;/td&gt;
  </code></pre>
  
  <p>I then recalculate the total cell using JQuery and click a Save button to POST the data to an Action.</p>
  
  <p>The problem I have is the edited Total does not post correctly, it becomes 0, while the rest of the data POSTS just fine.</p>
  
  <p>If I don't change the discount value, the Total from the model returned from the PartialViewResult Action POSTS correctly.</p>
  
  <p>I'm not sure if I have to set the hidden value again in JQuery or how I should do it.</p>
  
  <p>Below is the JQuery to edit the Total:</p>
  
  <pre><code>$(document).ready(function() {
      $(".discount").on("change", function() {
          var discountValue = $(this).closest('input');
  
          var price = $(this).closest('tr').find('td:eq(5)').text();
          var qty = $(this).closest('tr').find('td:eq(6)').text();
          var discount = ((price * qty) * discountValue / 100);
          var total = (price * qty) - discount;
  
          $(this).closest('tr').find('td:eq(8)').html(total.toFixed(2));
      });
  });
  </code></pre>