使用foreach循环来增加变量

时间:2012-02-05 10:03:58

标签: asp.net-mvc-3 razor for-loop foreach

我正在尝试将这些变量循环到下面(item_name,数量和金额)

@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"];
        paypal.@return = 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 => model.@return)
        @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

中的PayPal方法

1 个答案:

答案 0 :(得分:0)

我认为您可能需要的是:

@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
    }