0

I made a foreach loop that loops through all cart_items from a specific user. I need to calculate every product total price (product price * quantity) and store that in a viewbag to show on the page itself. I don't know how I can solve this issue though. This is what I've coded (the viewbag gets overrided, so the last row, that's the value it will show):

 public ActionResult Index()
    {
        string user_id = User.Identity.GetUserId();

        var order_id = db.Orders.Where(x => x.User_Id == user_id).Where(x => x.Paid == 0).Select(x => x.Order_Id).FirstOrDefault();
        var select_user_cart = db.Order_details.Where(x => x.Order_Id == order_id);




        var quantity_price = from x in db.Order_details
                             where x.Order_Id == order_id
                             select new { x.Quantity, x.Current_price};


        List<decimal> Calc = new List<decimal>();
        foreach (var item in quantity_price)
        {
            var quantity = item.Quantity.ToString();
            var double_quantity = Convert.ToDecimal(quantity);
            var double_price = Convert.ToDecimal(item.Current_price);

            var calc = double_quantity * double_price;
            Calc.Add(calc);               
        }
        ViewBag.Calc = Calc;


        var order_details = db.Order_details.Include(o => o.Product).Include(o => o.Order);


        return View(select_user_cart.ToList());
    }

Table

Code

Update

CodingLegend
  • 31
  • 1
  • 5

5 Answers5

0

Assuming what you want is a total price of all items, you need to add to a variable for each item and add the total to viewbag instead.

    double total = 0;
    foreach (var item in quantity_price)
    {
        var quantity = item.Quantity.ToString();
        var double_quantity = Convert.ToDouble(quantity);
        var double_price = Convert.ToDouble(item.Current_price);

        var calc = double_quantity * double_price;
        total += calc;
    }
    ViewBag.Calc = total;
Steve Harris
  • 5,014
  • 1
  • 10
  • 25
  • I have edited my post, the table is as follows: I want the price per product mutliplied by it's quantity. And if I have these values, it's also an easy calc I would say to get the real total of all prices. – CodingLegend Jun 01 '18 at 09:16
0

You can change your code to:

ViewBag.Calc = from x in order_details
where x.Order_Id == order_id
select (x.Quantity * x.Current_price);
Pang
  • 9,564
  • 146
  • 81
  • 122
  • This doesn't work because, one is an integer and the other one a string. Should I change Current_price datatype to decimal instead? So it can do it? – CodingLegend Jun 01 '18 at 09:23
  • Use ViewBag.Calc = from x in order_details where x.Order_Id == order_id select (Convert.ToDouble(x.Quantity) * Convert.ToDouble(x.Current_price)); – Smriti Singh Jun 01 '18 at 10:29
0

Please follow below steps to get viewbag list value.

Define a counter before the foreach loop.

Get item on the basis of counter.

Increase counter.

like this

@int i=0; //counter
@foreach (var item in Model)
{
   <tr>    
    <td>@ViewBag.Calc[i]</td>  //get item from the list basis of counter.
   @i++; //increase conter value
  </tr> 
}

Alternative you can add a new property to the model and set that property in the place of the viewbag and use as you are using other properties of the model.

Sain Pradeep
  • 3,119
  • 1
  • 22
  • 31
0

You can use a property name total in your model (ex: quantity, current_price) and assign the total value to it. Or you can use ViewBag.Calc as an array, but I think if you do use array it could also result in a problem. Make a list of type decimal or something like this:

List <decimal> total = new <decimal>();
foreach (var item in quantity_price)
{
    var quantity = item.Quantity.ToString();
    var double_quantity = Convert.ToDouble(quantity);
    var double_price = Convert.ToDouble(item.Current_price);

    var calc = double_quantity * double_price;
    total.add(calc)
}

ViewBag["calc"] = total;

In view:

@int count=0; 
@foreach (var item in Model)
{

<tr>
    <td>@ViewBag.Calc[count]</td>  
   @count++;
  </tr>

}
Krypt1
  • 1,066
  • 8
  • 14
0

I got it to work with the following code. The only thing that happens right now, is that the counter actually shows behind the price. But the calculatians are correct. Thanks to everyone that helped me. (If you know a fix on how to disable showing that counter, I'd love to hear it.)

VIEW:

@{ int counter = 0;
    foreach (var item in Model)
    {

<tr>
    <td>
        @Html.DisplayFor(modelItem => item.Product.Name)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Current_price)
    </td>
    <td>
        @Html.DisplayFor(modelItem => item.Quantity)
    </td>
    <td>
        @ViewBag.Calc[counter]
        @(counter++)

    </td>

    <td>
        @Html.ActionLink("Details", "Details", new { id = item.Order_details_Id }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.Order_details_Id })
    </td>
</tr>
        }
    } 

CONTROLLER:

List<decimal> Calc = new List<decimal>();
        foreach (var item in quantity_price)
        {
            var quantity = item.Quantity.ToString();
            var double_price = Convert.ToDecimal(item.Current_price);
            var calc = item.Quantity * double_price;
            Calc.Add(calc);               
        }
        ViewBag.Calc = Calc;
CodingLegend
  • 31
  • 1
  • 5