2017-06-16 86 views
0

我想創建一個購物車,但在嘗試ID傳遞,因爲這一種說法是發生什麼事MVC傳遞屬性作爲參數

http://localhost:15359/RealCartController/Order/FinalProject.Models.Clothes

HTTP錯誤404.0 - 找不到 你正在尋找的資源已被刪除,名稱已更改或暫時不可用。

這是RealCartController

public ActionResult Order(int id) 
      { 
       if(Session["cart"] == null) 
       { 
        List<Item> cloth = new List<Item>(); 
        cloth.Add(new Item(de.Clothes.Find(id),1)); 
        Session["cart"] = cloth; 
       } 
       else 
       { 
        List<Item> cart = (List<Item>)Session["cart"]; 
        int index = isExisting(id); 
        if (index == -1) 
         cart.Add(new Item(de.Clothes.Find(id), 1)); 
        else 
         cart[index].Amount++; 

        Session["cart"] = cart; 
       } 
       return View(); 
      } 

這是在調用該函數

@if (Model != null) 
{ 

    foreach (Clothes Id in Model) 
    { 
     if (Id.Amount > 0) 
     { 

     <div id="container"> 
      <div id="row"> 

       <img src="~/Content/img/@Html.DisplayFor(model => Id.ImagePath)" style="height:200px;width:200px;" /> 
       <br /> 
       <br /> 
        @Html.ActionLink("Add To Cart", "Order", "RealCartController", new { id = Id }, null); 
      </div> 
     </div> 
     } 

    } 
} 

查看這是函數

@foreach (Item item in (List<Item>)Session["cart"]) 
     { 

      <tr> 
       <td>@item.Cl.Id</td> 
       <td>@item.Cl.Price</td> 
       <td>@item.Cl.Amount</td> 
       <td>@item.Cl.Price * item.Cl.Amount</td> 
      </tr> 
     } 
+0

我確認鏈接提供的項目的主鍵給出了404錯誤:=) –

+0

鏈接指向位於本地計算機上的* localhost *。如果我們沒有配置設置或文件位於我們的機器中,我們無法測試它的有效性。 : - \ – Sometowngeek

+0

@LaurentLequenne和Sometowngeek將它自動格式化爲一個鏈接,並且URL中的信息是相關的。 – NtFreX

回答

0

變量Id中的視圖這條線foreach (Clothes Id in Model)是一個對象,而不是一個數字。當您嘗試將對象作爲參數發送給ActionLink函數時,它只會發送對象類名稱。因此,api調用無法解析參數。

你可以看到一個類名是在url中發送的。

http://localhost:15359/RealCartController/Order/FinalProject.Models.Clothes

FinalProject.Models.Clothes部分應該是訂單的ID號來代替。

http://localhost:15359/RealCartController/Order/1

0

它看起來應該像這樣

@Html.ActionLink("Add To Cart", "Order", "RealCartController", new { id = 
    Id.ProductId }, null); 

產品編號是從你的類/模型,併爲您的