2012-05-16 26 views
0

我有一個產品(圖像)列表的產品(圖像),用戶可以滾動(我使用jquery)&他們是id與每個圖像關聯,我想如果用戶點擊按鈕(添加到購物車),然後產品應該添加到購物車。我不知道如何獲得當前顯示產品的ID。請幫助我是MVC的新手。如何在asp.net mvc中獲取當前正在查看的產品的ID?

查看

    //此處圖片
@using (Html.BeginForm("Index","Product")) 
{ 
    <input type=submit value="Add To Cart"/> 
} 

控制器

[HttpPost] 
    public ActionResult Index(MyUsers user) 
    { 
     string s = user.Name; 
     return View(); 
    } 
+0

使用get方法。示例:/ cart/add/

+0

是否想要添加到購物車按鈕的所有產品? – archil

+0

是的,這就是主要問題 –

回答

1

如果有一個按鈕 「添加到圖表」 每個產品相關聯的,與相應的產品添加一個隱藏字段每個這些按鈕旁邊的ID。

@using (Html.BeginForm("Index","Product")) 
{ 
    @Html.Hidden("ProductID", productID) 
    <input type="submit" value="Add To Cart"/> 
} 

然後將參數ProductID添加到操作方法中。

[HttpPost] 
public ActionResult Index(MyUsers user, int productID) 
{ 
    // Your code... 
} 
+0

但如果用戶刷卡產品圖像,然後 –

+0

,我試了它,productID始終爲空, –

+0

根據您原來的問題下的評論,你想只有一個提交按鈕,你選擇圖片以某種方式使用jQuery。那麼您可以使用jQuery更改隱藏字段的值。 –

0

查看:

@using (Html.BeginForm()) 
{ 
    foreach(Product product in Model) 
    { 
     @Html.LabelFor(model => product.Name) 
     @Html.ActionLink("Add To Cart", "Add", "Product", new { id = product.Id }) 
    } 
} 

控制器:

public ActionResult Add(int id) 
{ 
    // Your code... 
} 
相關問題