私の質問は、actionlinkをクリックすると、ビューは特定のIDをコントローラ(例:ProductID = 6)に送信しますが、コントローラは特定のIDデータではないすべてのデータを取得します。ラムダ式問題
私は問題がコントローラーのラムダ式だと思うので、すべてのデータを私に渡します。
これらは私のモデルです:
public class ShoppingCart
{
public List<ShoppingCartItemModel> items = new List<ShoppingCartItemModel>();
public IEnumerable<ShoppingCartItemModel> Items
{
get { return items; }
}
}
public class ShoppingCartItemModel
{
public Product Product
{
get;
set;
}
public int Quantity { get; set; }
}
コントローラー:あなたはでcart.ItemsにLINQクエリからの戻り値を保持する必要が
[HttpGet]
public ActionResult EditFromCart(int ProductID)
{
ShoppingCart cart = GetCart();
cart.items.Where(r => r.Product.ProductID == ProductID)
.Select(r => new ShoppingCartItemModel
{
Product = r.Product,
Quantity = r.Quantity
});
return View(cart);
//return RedirectToAction("Index", "ShoppingCart");
}
private ShoppingCart GetCart()
{
ShoppingCart cart = (ShoppingCart)Session["Cart"];
//如果現有購物車中已經沒有任何內容
if (cart == null)
{
//產生新購物車物件
cart = new ShoppingCart();
//用session保存此購物車物件
Session["Cart"] = cart;
}
//如果現有購物車中已經有內容,就傳回 view 顯示
return cart;
}
ビュー
@model ShoppingCart
@{
ViewBag.Title = "購物車內容";
}
<h2>Index</h2>
<table class="table">
<thead>
<tr>
<th>
Quantity
</th>
<th>
Item
</th>
<th class="text-right">
Price
</th>
<th class="text-right">
Subtotal
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.items)
{
<tr>
<td class="text-center">
@item.Quantity
</td>
<td class="text-center">
@item.Product.ProductName
</td>
<td class="text-center">
@item.Product.Price.ToString("c")
</td>
<td class="text-center">
@((item.Quantity * item.Product.Price).ToString("c"))
</td>
<td>
@using (Html.BeginForm("RemoveFromCart", "ShoppingCart"))
{
@Html.Hidden("ProductId", item.Product.ProductID)
@*@Html.HiddenFor(x => x.ReturnUrl)*@
<input class="btn btn-warning" type="submit" value="Remove">
}
</td>
<td>
@using (Html.BeginForm("EditFromCart", "ShoppingCart", FormMethod.Get))
{
@Html.Hidden("ProductId", item.Product.ProductID)
<input class="btn btn-warning" type="submit" value="Edit">
}
</td>
</tr>
}
</tbody>
</table>
あなたは全体のカートの価値を表示するために – Vicky