0

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();

    }
tereško
  • 58,060
  • 25
  • 98
  • 150
salman
  • 53
  • 2
  • 7
  • 2
    *"How to pass value from one view to another in MVC"*, *"how to pass values from one form to another in c#"* ..etc... I read 10s of questions every day. Isn't there any good answer for it after so many years... – Eser Mar 07 '16 at 20:58
  • how you call Payment action? can you add the code for it? – Monah Mar 07 '16 at 21:00
  • This is not a problem, it is fundamental learning. I suggest you read further about passing values or using ViewBag or ViewData. – Aizen Mar 07 '16 at 21:01
  • 1
    Possible duplicate of [how to pass values from one view to another view](http://stackoverflow.com/questions/27517171/how-to-pass-values-from-one-view-to-another-view) – Eugene Podskal Mar 07 '16 at 21:02
  • Probably you don't want to pass it, the best thing would be to simply recalculate it in the Payment action. – kamilk Mar 07 '16 at 21:39

1 Answers1

1

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);
Andy
  • 83
  • 7