In my Razor view i used a List of items with an option to select / deselect. The model i used is
public class BorrowedItemModel
{
public long Id { get; set; }
public bool IsSelected { get; set; }
public string Item { get; set; }
public double Total{get;set;}
}
And i want to show the Grand Total as the Sum of all selected items ( If user deselects any item i want to update Grand total as well). Can i do this with Razor inside a Javascript like function? I tried this code but not showing result
@model IList<RoyaltyDb.Models.BorrowedItemModel>
<script type="text/javascript">
$(document).ready(function () {
$('.selectionItem').change(function() {
recalculate();
});
});
function recalculate()
{
var total_cal=0;
@{
double total = 0;
foreach(var item in Model)
{
if (item.IsSelected)
{
total += item.Royalty;
}
}
}
//Can i asssign this grand total outside here ??
}
</script>
<div class="col-md-8">
<table id="borrowedtexts" class="table table-striped">
<thead>
<tr>
<th>
Select
</th>
<th>
@Html.DisplayNameFor(model => model[0].Item)
</th>
<th>
@Html.DisplayNameFor(model => model[0].Total)
</th>
</tr>
</thead>
<tbody>
@for (int item = 0; item < Model.Count(); item++)
{
<tr>
<td>
@Html.CheckBoxFor(modelItem => Model[item].IsSelected, new { @class="selectionItem"})
@Html.HiddenFor(modelItem => Model[item].Id)
</td>
<td>
@Html.DisplayFor(modelItem => Model[item].Item, new { disabled = "true" })
</td>
<td>
@Html.DisplayFor(modelItem => Model[item].Total, new { disabled = "true" })
</td>
</tr>
}
</tbody>
</table>
</div>
<div class="col-md-4">
//Want to show grand total here...
</div>