2016-02-25 102 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

沒問題。很高興工作! –