我在“产品详细信息”视图中创建了一个dropdownlistfor,它具有5个硬编码值(1、2、3、4、5),当我选择该值时,我希望将其传递到ShoppingCart控制器中的AddToCartWithQuantity方法中,然后,当我单击“添加到购物车”按钮时,使用相同名称的购物车模型方法,但是当我将商品添加到购物车时,数量变为0。
我的产品详细信息视图:
@model BigVisionGames.Models.Products
@{
ViewBag.Title = "Details";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h3>Product: @Model.ProductName</h3>
<div id="product-details">
<p>
<em>Price: </em>
£@($"{Model.Price:F}")
</p>
<div style="color:black">
@Html.DropDownListFor(model => model.ChosenQuantity, new
List<SelectListItem>
{
new SelectListItem{ Text="1", Value = "1" },
new SelectListItem{ Text="2", Value = "2" },
new SelectListItem{ Text="3", Value = "3" },
new SelectListItem{ Text="4", Value = "4" },
new SelectListItem{ Text="5", Value = "5" }
})
@Html.ValidationMessageFor(model => model.ChosenQuantity, "", new { @class = "text-danger" })
</div>
@if (Model.StockLevel == 0)
{
<p style="color:red">This item is currently out of stock!</p>
}
else
{
<p class="btn">
@Html.ActionLink("Add to cart", "AddToCartWithQuantity", "ShoppingCart", new { id = Model.Id}, "")
</p>
}
ShoppingCartController方法
public ActionResult AddToCartWithQuantity(int id, Products productModel)
{
// Retrieve the product from the database
var addedproduct = storeDB.Products
.Single(product => product.Id == id);
// Add it to the shopping cart
var cart = ShoppingCart.GetCart(this.HttpContext);
cart.AddToCartWithQuantity(addedproduct, productModel.ChosenQuantity);
// Go back to the main store page for more shopping
return RedirectToAction("Index");
}
相关分类