The easiest way would be to add an identifier to each textbox so that you can use jQuery to easily find them and get the current values. I recommend a class because those can be repeated without generating invalid HTML.
@for (int count = 0; count < Model.Bill.Count(); count++)
{
<tr>
<td>
@Html.LabelFor(m => m.Bill[count].BillDenom)
</td>
<td>
@Html.TextBoxFor(m => m.Bill[count].Count, new {@class = "Count"})
</td>
<td>
@Html.TextBoxFor(m => m.Amount, new {@class = "Amount"})
</td>
</tr>
}
<!--Create placeholders with IDs for the total amounts-->
<span id="TotalCount"></span>
<span id="TotalAmount"></span>
Now, we need to add handlers to each textbox so that jQuery can know when it needs to calculate an updated amount.
<script type="text/javascript">
$(function(){
$(".Amount").on("focusout", function(){
RecalculateItems();
});
$(".Count").on("focusout", function(){
RecalculateItems();
});
})
</script>
Lastly, we need to implement the RecalculateItems() function which will loop through all the items and sum them up accordingly.
function RecalculateItems(){
var totalCount = 0;
var totalAmount = 0;
$(".Count").each(function(){
//loop through each item with the class Count, parse as integer and add to running total
totalCount += parseInt($(this).val());
});
$(".Amount").each(function(){
//same as above, except with items with Amount class
totalAmount += parseInt($(this).val());
});
//set inner html of each span identified above with the correct values
$("#TotalAmount").html(totalAmount);
$("#TotalCount").html(totalCount);
}
$("tr").each()to loop through the rows, and add the amounts and totals to variables.