2016-02-25 111 views
1

我对MVC有点新。但我已经走了很长的路。我将PayPal MVC API集成到了我的项目中,并且我意识到当我将多个项目放入购物车时,检查项目列表时只会填充数组中的最新项目。如何添加项目到贝宝API中的项目列表

我一直在尝试玩这个BU我很容易不知道我错过了将多个项目添加到贝宝项目列表中。

有这部分PaymentWithCreditCard():

 //create and item for which you are taking payment 
     //if you need to add more items in the list 
     //Then you will need to create multiple item objects or use some loop to instantiate object 
     var item = new Item(); 
     foreach (var cartitem in cookieCarts) 
     { 
      item.name = cartitem.Fullname; 
      item.currency = "USD"; 
      item.price = cartitem.Price; 
      item.quantity = cartitem.Qty.ToString(); 
      item.sku = cartitem.Sku; 
      var intPrice = Int32.Parse(cartitem.Price); 
      subtotal = subtotal + intPrice; 
     } 

     //Now make a List of Item and add the above item to it 
     //you can create as many items as you want and add to this list 
     var itms = new List<Item>(); 
     itms.Add(item); 
     var itemList = new ItemList(); 
     itemList.items = itms; 

林不知道如何去增加我的for循环到项目列表

回答

3

@NeoSketo尝试。另外,我不明白小计在做什么,所以我把它放在一边。

  List<Item> items = new List<Item>(); 

      foreach (var cartitem in cookieCarts) 
      {      
       items.Add(new Item { 
        name = cartitem.FullName, 
        currency = "USD", 
        price = cartitem.Price, 
        sku = cartitem.Sku, 
        quantity = cartitem.Qty.ToString() 
       }); 

       var intPrice = Int32.Parse(cartitem.Price); 
       subtotal = sobtotal + intPrice; 
      } 

      ItemList theItemList = new ItemList(); 
      theItemList.items = new List<Item>(); 
      theItemList.items = items; 
+2

不错!非常感谢!我看到了!小计只是通过迭代来计算价格。 – NeoSketo

+1

没问题。很高兴工作! –