Reputation: 53
I would like to display the total amount (@totalAmount) from the index and show it on the payment page which is the next page. I am trying to use a viewbag but it doesn't seem to work.
This is my index...
@model IEnumerable<charity.Models.Donation>
@{
ViewBag.Title = "Index";
ViewBag.total = "@totalAmount";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.DisplayName)
</th>
<th>
@Html.DisplayNameFor(model => model.Date)
</th>
<th>
@Html.DisplayNameFor(model => model.Amount)
</th>
<th>
@Html.DisplayNameFor(model => model.TaxBonus)
</th>
<th>
@Html.DisplayNameFor(model => model.Comment)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.DisplayName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Date)
</td>
<td>
@Html.DisplayFor(modelItem => item.Amount)
</td>
<td>
@Html.DisplayFor(modelItem => item.TaxBonus)
</td>
<td>
@Html.DisplayFor(modelItem => item.Comment)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", new { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ID })
</td>
</tr>
}
</table>
@{
Decimal totalAmount = 0;
if (Model != null)
{
totalAmount = Model.Sum(x => x.Amount);
}
<label>Total amount donated</label>
<h1 id="amount">@totalAmount</h1>
}
@{
Decimal totaltax = 0;
if (Model != null)
{
totaltax = Model.Sum(x => x.TaxBonus);
}
<label>Total tax bonus</label>
<h1 name="amount">@totaltax</h1>
}
This is my payment page...
@{
ViewBag.Title = "Payment";
}
<h2>Payment</h2>
<form>
<h1>@ViewBag.total</h1>
</form>
Index and payment code from the controller...
public ActionResult Index()
{
return View(db.Donations.ToList());
}
public ActionResult Payment() {
return View();
}
Upvotes: 0
Views: 6997
Reputation: 83
Try to use a Session Variable instead of your ViewBag. It will store the total as an int.
Session["totalAmount"] = Model.Sum(x => x.Amount);
Upvotes: 1