我正在使用 mvc,我正在尝试将我的整个购物车发送到贝宝。我可以让它添加单个项目,但不能添加整个购物车。
这是我的控制器:
public ActionResult PostToPayPal(Cart cart, ShippingDetails shippingInfo)
{
PayPal paypal = new PayPal();
paypal.cmd = "_xclick";
paypal.business = ConfigurationManager.AppSettings["BusinessAccountKey"];
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 = System.Configuration.ConfigurationManager.AppSettings["CancelURL"];
paypal.@return = ConfigurationManager.AppSettings["ReturnURL"];
paypal.notify_url = ConfigurationManager.AppSettings["NotifyURL"];
paypal.currency_code = ConfigurationManager.AppSettings["CurrencyCode"];
foreach (var lines in cart.Lines )
{
paypal.item_name = lines.Product.Name;
paypal.amount = lines.Product.Price.ToString("c");
}
//paypal.item_name = item;
//paypal.amount = amount;
return View(paypal);
}
这是我的看法:
@model SportsStore.Domain.Entities.PayPal
@{
ViewBag.Title = "PostToPayPal";
}
<h2>PostToPayPal</h2>
<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.amount)
<p>
<h4>redirecting to paypal...</h4>
</p>
</form>
<script type="text/javascript" language="javascript">
$(this.document).ready(function () {
var frm = $("form");
frm.submit();
})
</script>
这是我的购物车:
@model SportsStore.WebUI.Models.CartIndexViewModel
@{ ViewBag.Title = "小弓窥视:你的购物车"; }
您的购物车
<table width="90%" align="center">
<thead>
<tr>
<th align="center">Quantity</th>
<th align="left">Item</th>
<th align="right">Price</th>
<th align="right">Subtotal</th>
</tr>
</thead>
<tbody>
@foreach(var line in Model.Cart.Lines)
{
<tr>
<td align="center">@line.Quantity</td>
<td align="left">@line.Product.Name</td>
<td align="right">@line.Product.Price.ToString("c")</td>
<td align="right">@((line.Quantity * line.Product.Price).ToString("c"))</td>
<td>
@using(Html.BeginForm("RemoveFromCart", "Cart"))
{
@Html.Hidden("ProductId", line.Product.ProductID)
@Html.HiddenFor(x => x.ReturnUrl)
<input class="actionButtons" type="submit" value="Remove"/>
}
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td colspan="3" align="right">Total:</td>
<td align="right">
@Model.Cart.ComputeTotalValue().ToString("c")
</td>
</tr>
</tfoot>
</table>
<p align="center" class="actionButtons">
<a href="@Model.ReturnUrl">Continue Shopping</a>
@Html.ActionLink("Checkout now", "Checkout")
</p>
购物车实体的代码:
private readonly List<CartLine> lineCollection = new List<CartLine>();
public IEnumerable<CartLine> Lines
{
get { return lineCollection; }
}
public void AddItem(Product product, int quantity)
{
CartLine line = lineCollection
.Where(p => p.Product.ProductID == product.ProductID)
.FirstOrDefault();
if (line == null)
{
lineCollection.Add(new CartLine {Product = product, Quantity = quantity});
}
else
{
line.Quantity += quantity;
}
}
public void RemoveLine(Product product)
{
lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
}
public decimal ComputeTotalValue()
{
return lineCollection.Sum(e => e.Product.Price*e.Quantity);
}
public void Clear()
{
lineCollection.Clear();
}
}
公共类 CartLine { 公共产品产品 { 获取;放; } 公共 int 数量 { 获取;放; } }
有什么帮助吗???
谢谢...