2012-05-16 30 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... 
} 
相关问题