0

我有一个用户可以滚动的产品(图像)列表(我使用 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();
    }
4

2 回答 2

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...
}
于 2012-05-16T09:00:18.467 回答
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...   
} 
于 2012-05-16T09:49:56.673 回答