如何在asp.net mvc中获取当前查看产品的ID?

时间:2012-05-16 08:13:00

标签: asp.net-mvc asp.net-mvc-3

我有一个用户可以滚动的产品(图像)列表(我使用Jquery)&他们是与每个图像相关联的id,我想如果用户点击按钮(添加到购物车),那么产品应该添加到购物车。我不知道如何获取当前显示产品的ID。请帮助我是MVC的新手。

查看

    //图像在这里
@using (Html.BeginForm("Index","Product"))
{
    <input type=submit value="Add To Cart"/>
}

控制器

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

2 个答案:

答案 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...
}

答案 1 :(得分: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...   
} 
相关问题