2012-02-05 209 views
-1

我想下面的循環(ITEM_NAME,數量和金額),這些變量使用foreach循環遞增變量

@using (Html.BeginForm("PostToPaypal", "ShoppingCart")) 
{ 

for (int i = 0; i < 10; i++) 
{ 
    foreach (var item in Model.CartItems) 
    { 
    <input type="hidden" name="item_name" value="@item.Product.Title"/> 
    <input type="hidden" name="quantity" value="@item.Count" /> 
    <input type="hidden" name="amount" value="@item.Product.Price"/> 
    } 
} 

    <input type="submit" name="btnsubmit" value="Pay with PayPal" /> 
} 


我不知道如何合併這兩個for循環。
而且我不知道如何在「輸入類型字段」使用「INT I ..」

可能有人請給我(代碼)解釋我是如何做到這一點?

ShoppingCartController;

public ActionResult PostToPaypal(string item_name, string quantity,string amount) 
    { 
     ESpiceHerbs.Models.PayPal paypal = new Models.PayPal(); 

     paypal.cmd = "_xclick"; 
     paypal.business = ConfigurationManager.AppSettings["BusinessAccountKey"]; 
     paypal.no_shipping = "1"; 
     bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["UseSandBox"]); 
     if (useSandbox) 
      ViewBag.actionURL = "https://www.sandbox.paypal.com/cgi-bin/webscr"; 
     else 
      ViewBag.actionURL = "https://www.paypal.com/cgi-bin/webscr"; 

     paypal.cancel_return = ConfigurationManager.AppSettings["CancelURL"]; 
     [email protected] = ConfigurationManager.AppSettings["ReturnURL"];//+"&PaymentId=1"; you can append your order Id here 
     paypal.notify_url = ConfigurationManager.AppSettings["NotifyURL"]; // +"?PaymentId=1"; to maintain database logic 

     paypal.currency_code = ConfigurationManager.AppSettings["CurrencyCode"]; 

     paypal.item_name = item_name; 
     paypal.quantity = quantity; 
     paypal.amount = amount; 
     return View(paypal); 
    } 

PayPal.Model

public class PayPal 
    { 
    public string cmd { get; set; } 
    public string business { get; set; } 
    public string no_shipping { get; set; } 
    public string @return { get; set; } 
    public string cancel_return { get; set; } 
    public string notify_url { get; set; } 
    public string currency_code { get; set; } 
    public string item_name { get; set; } 
    public string quantity { get; set; } 
    public string amount { get; set; } 
} 

而且PostToPayPal.cshtml

<form id="frm" action="@ViewBag.actionURL"> 
     @Html.HiddenFor(model => model.cmd) 
     @Html.HiddenFor(model => model.business) 
     @Html.HiddenFor(model => model.no_shipping) 
     @Html.HiddenFor(model => [email protected]) 
     @Html.HiddenFor(model => model.cancel_return) 
     @Html.HiddenFor(model => model.notify_url) 
     @Html.HiddenFor(model => model.currency_code) 
     @Html.HiddenFor(model => model.item_name) 
     @Html.HiddenFor(model => model.quantity) 
     @Html.HiddenFor(model => model.amount) 
    </form> 

我已經提到,從http://www.arunrana.net/2012/01/paypal-integration-in-mvc3-and-razor.html

回答

0

PayPal的方法,我想你可能需要的是這樣的:

@using (Html.BeginForm("PostToPaypal", "ShoppingCart")) 
{ 

    for (int i=0; i<Model.CartItems.Count; i++) 
    { 
     @Html.HiddenFor(m => m.CartItems[i].Product.Title) 
     @Html.HiddenFor(m => m.CartItems[i].Count) 
     @Html.HiddenFor(m => m.CartItems[i].Product.Price) 
    } 

<input type="submit" name="btnsubmit" value="Pay with PayPal" /> 

}

,然後在PostToPaypalAction

[HttpPost] 
    public ActionResult PostToPaypal(MODELTYPEHERE[] model) 
    { 
     //processing here 
    } 
+0

我想你的方法。沒有錯誤被返回。但是,當我結賬到PayPal時,沒有值被傳遞。請問您能更具體地瞭解我應該在控制器中做什麼?謝謝!! – 2012-02-05 11:47:23

+0

@Mohamed Azlan - 你可以發表你正在使用的模型和你的問題中的控制器動作,以便我可以更好地理解問題的可能性。謝謝。 – Dangerous 2012-02-05 11:56:34

+0

我通過編輯帖子添加了Controller,Model和Cshtml頁面。感謝您對此進行調查。 – 2012-02-05 12:05:49