ASP.net Foreach 循环并将数据存储在 viewbag 中以显示在视图中

我做了一个 foreach 循环,循环遍历来自特定用户的所有 car_items。我需要计算每个产品的总价(产品价格 * 数量)并将其存储在查看袋中以显示在页面本身上。我不知道如何解决这个问题。这是我编码的内容(viewbag 被覆盖,所以最后一行,这就是它将显示的值):


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

    }

http://img.mukewang.com/60f287830001bff012380195.jpg

http://img.mukewang.com/60f2878d0001e10b19100374.jpg

http://img4.mukewang.com/60f2879600013ca519130370.jpg

慕斯709654
浏览 492回答 3
3回答

手掌心

请按照以下步骤获取 viewbag 列表值。在 foreach 循环之前定义一个计数器。根据计数器获取项目。增加计数器。像这样@int i=0; //counter@foreach (var item in Model){&nbsp; &nbsp;<tr>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <td>@ViewBag.Calc[i]</td>&nbsp; //get item from the list basis of counter.&nbsp; &nbsp;@i++; //increase conter value&nbsp; </tr>&nbsp;}或者,您可以向模型添加新属性并在视图包的位置设置该属性,并像使用模型的其他属性一样使用。

慕无忌1623718

您可以在模型中使用属性名称总计(例如:数量、当前价格)并为其分配总值。或者您可以ViewBag.Calc用作数组,但我认为如果您确实使用数组,它也可能导致问题。制作一个十进制类型的列表或类似的东西:List <decimal> total = new <decimal>();foreach (var item in quantity_price){&nbsp; &nbsp; var quantity = item.Quantity.ToString();&nbsp; &nbsp; var double_quantity = Convert.ToDouble(quantity);&nbsp; &nbsp; var double_price = Convert.ToDouble(item.Current_price);&nbsp; &nbsp; var calc = double_quantity * double_price;&nbsp; &nbsp; total.add(calc)}ViewBag["calc"] = total;鉴于:@int count=0;&nbsp;@foreach (var item in Model){<tr>&nbsp; &nbsp; <td>@ViewBag.Calc[count]</td>&nbsp;&nbsp;&nbsp; &nbsp;@count++;&nbsp; </tr>}

慕尼黑5688855

假设您想要的是所有商品的总价,您需要为每个商品添加一个变量,然后将总价添加到 viewbag。&nbsp; &nbsp; double total = 0;&nbsp; &nbsp; foreach (var item in quantity_price)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var quantity = item.Quantity.ToString();&nbsp; &nbsp; &nbsp; &nbsp; var double_quantity = Convert.ToDouble(quantity);&nbsp; &nbsp; &nbsp; &nbsp; var double_price = Convert.ToDouble(item.Current_price);&nbsp; &nbsp; &nbsp; &nbsp; var calc = double_quantity * double_price;&nbsp; &nbsp; &nbsp; &nbsp; total += calc;&nbsp; &nbsp; }&nbsp; &nbsp; ViewBag.Calc = total;
打开App,查看更多内容
随时随地看视频慕课网APP