I am using a partial view and JQuery to dynamically add rows to a table.
Basically very similar to MVC 5 BeginCollectionItem with Partial CRUD
I have a Total column that is simply the price * qty.
another cell is using @Html.TextBoxFor to enter a discount percent.
<td>
@Html.TextBoxFor(m => m.PercentDiscount, new {@class = "form-control discount", type = "number"})
@Html.HiddenFor(m => m.PercentDiscount)
</td>
I then recalculate the total cell using JQuery and click a Save button to POST the data to an Action.
The problem I have is the edited Total does not post correctly, it becomes 0, while the rest of the data POSTS just fine.
If I don't change the discount value, the Total from the model returned from the PartialViewResult Action POSTS correctly.
I'm not sure if I have to set the hidden value again in JQuery or how I should do it.
Below is the JQuery to edit the Total:
$(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));
});
});