I am trying to loop these variables below (item_name, quantity and amount)
@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" />
}
I do not know how to merge these two for loops.
And i do not know how to use the “int i ..” in the “input type field”
Could somebody please explain to me (in code) how i can achieve this?
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; }
}
And 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>
I have referenced the PayPal method from http://www.arunrana.net/2012/01/paypal-integration-in-mvc3-and-razor.html
I think what you might need is this:
}
And then on the
PostToPaypalAction: