2016-09-29 88 views
1

我的问题是当我点击actionlink时,视图发送特定的ID给控制器(例如ProductID = 6),但我的控制器抓取所有数据给我而不是特定的ID数据。lambda表达式问题

我认为问题是控制器的lambda表达式,它会将所有数据都抓到我。

这是我的模型:

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; } 
    } 

控制器:

 [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> 
+0

你正在通过整车的价值来查看 – Vicky

回答

0

您必须像这样将过滤值分配给购物车。

cart.item = cart.items.Where(r => r.Product.ProductID == ProductID) 
         .Select(r => new ShoppingCartItemModel 
             { 
              Product = r.Product, 
              Quantity = r.Quantity 
             }).ToList(); 

使用ToList();FirstOrDefault()按你的条件

+0

非常有用的感谢 –

0

您需持在cart.Items从LINQ查询返回的值变量并将其传递给View方法。 目前,您的查询结果正在丢失,整个购物车传递给View方法。

+0

如何做到这一点?你能给我一个简单的例子吗?谢谢 –

2

的关键问题是,这个代码不返回LINQ查询的结果,因为你还没有分配一个变量的结果:

cart.items.Where(r => r.Product.ProductID == ProductID) 
         .Select(r => new ShoppingCartItemModel 
             { 
              Product = r.Product, 
              Quantity = r.Quantity 
             }); 

我强烈建议您专门创建一个视图模型来显示购物车物品。

public class CartItemsViewModel 
{ 
    public List<ShoppingCartItemModel> Items { get; set; } 
} 

[HttpGet] 
public ActionResult EditFromCart(int ProductID) 
{ 

    ShoppingCart cart = GetCart(); 

    var viewModel = new CartItemsViewModel(); 

    viewModel.Items.AddRange(cart.items.Where(r => r.Product.ProductID == ProductID) 
       .Select(r => new ShoppingCartItemModel 
           { 
            Product = r.Product, 
            Quantity = r.Quantity 
           })); 

    return View(viewModel); 
} 

在我的例子我用.AddRange()法取结果的LINQ要求对车的物品,并将它们存储在视图模型的Items财产。