我正在尝试使用ajax制作ShoppingCart。
我正在学习本教程。 http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-8
我想传递radiobutton值的值,而不是DB中的'price'值。
当我将产品添加到购物车时,URL将显示如下:
http://localhost:58759/ShoppingCart?priceValue=b
当我尝试删除'?priceValue = b'时,它将删除购物车页面中的价格值和总价值。在购物车表中,它已正确创建购物车列表,但在视图中,它不显示具有相同cartId的整个购物车列表,即使用SessionKey。
另外,首先添加产品后,当我尝试添加不同租期的同一产品时,它不会插入列表,而且只会更新购物车列表值。
那么如何插入不同租期的购物车?数据库部分没问题,但我认为在View和Controller中有关于传递参数的问题。
编码看起来太长了,真的很抱歉。
我需要你的帮助。我从2周前开始在那里买股票:(
首先,我的编码如何传递radioButtonValue:
Rental Period: <br />
@using (Html.BeginForm("AddToCart", "ShoppingCart", new { id = Model.productId }, FormMethod.Post))
{
<div class="display-label">
@Html.RadioButtonFor(model => model.price, "a")
3 day: £@Model.threeDayPrice
</div>
<div class="display-label">
@Html.RadioButtonFor(model => model.price, "b")
1 week: £@Model.aWeekPrice
</div>
<br />
<input type="submit" class="button" value="Add to cart" style="margin-left:0px; width:90px;"/>
<p class="button" style="width:90px;">
@Html.ActionLink("Back to List", "Index", "Home")
</p>
然后我传递的参数值如下:
[HttpPost]
public ActionResult AddToCart(int id, FormCollection col)
{
var addedProduct = db.Product
.Single(product => product.productId == id);
//this is radiobutton value
string priceValue = col["price"];
// Retrieve the product from the database
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCart(addedProduct);
// Go back to the main store page for more shopping
//I am trying to pass the value.
return RedirectToAction("Index", new { @priceValue = priceValue });
}
索引页面适用于:
public ActionResult Index(string priceValue)
{
var cart = ShoppingCart.GetCart(this.HttpContext);
// Set up our ViewModel
string price = priceValue;
ViewBag.eachPrice = price;
var viewModel = new ShoppingCartViewModel
{
CartItems = cart.GetCartItems(),
CartTotal = cart.GetTotal(price)
};
// Return the view
return View(viewModel);
}
这是getTotal方法:
public decimal GetTotal(string price)
{
// Multiply product price by count of that album to get
// the current price for each of those product in the cart
// sum all product price totals to get the cart total
if (price == "a")
{
decimal? total = (from cartItems in db.Cart
where cartItems.cartId == ShoppingCartId
select (int?)cartItems.count * cartItems.Product.threeDayPrice).Sum();
return total ?? decimal.Zero;
}
else if (price == "b")
{
decimal? total = (from cartItems in db.Cart
where cartItems.cartId == ShoppingCartId
select (int?)cartItems.count * cartItems.Product.aWeekPrice).Sum();
return total ?? decimal.Zero;
}
else{
return decimal.Zero;
}
}
然后当您看到购物车的“查看”页面时(它正在使用ShoppingCartViewMode):
@model MvcApplication2.ViewModels.ShoppingCartViewModel
@foreach (var item in Model.CartItems)
{
<tr id="row-@item.recordId">
<td>
@Html.ActionLink(item.Product.model, "Details", "Store", new { id = item.productId }, null)
</td>
<td>
@ViewBag.eachPrice
</td>
<td id="item-count-@item.recordId">
@item.count
</td>
<td>
@item.dateCreated.Date.ToString("dd MMM yyyy")
</td>
<td>
@*how about using some model instead of number?*@
@item.dateCreated.Date.AddDays(2).ToString("dd MMM yyyy")
</td>
<td>
</td>
<td>
<a href="#" class="RemoveLink" data-id="@item.recordId">
Remove from cart</a>
</td>
</tr>
}
ShoppingCartViewModel是:
public class ShoppingCartViewModel
{
public List<Cart> CartItems { get; set; }
public decimal CartTotal { get; set; }
}
你能反馈给我或任何帮助吗?谢谢你的阅读。谢谢。