我有一个 Orchard 项目,我在其中用 MVC 创建了一个模块。我想使用 @Html.ActionLink 将特定用户的 ID 传递给 Controller ,但它不调用 Controller 。这是我的代码:
在 View 中:
@Html.ActionLink("100111", "AddToCart", "ShoppingCart", new { id = 101 }, null)
//also tried,
@Html.ActionLink("102829", "AddToCart", "ShoppingCart", new { id = 1, area = "OnlineShopping" },null)
在 Controller 中:
[HttpPost]
public ActionResult AddToCart(int id)
{
_shoppingCart.Add(id, 1);
return RedirectToAction("Index");
}
[Themed]
public ActionResult Index()
{
// Create a new shape using the "New" property of IOrchardServices.
var shape = _services.New.ShoppingCart();
// Return a ShapeResult
return new ShapeResult(this, shape);
}
最佳答案
因为 [HttpPost]
而不是调用操作方法,点击 anchor 标记实际上执行 Get
。因此,请尝试从 AddToCart
操作中删除 [HttpPost]
属性装饰。
[HttpPost]//<--Remove this
public ActionResult AddToCart(int id)
{
_shoppingCart.Add(id, 1);
return RedirectToAction("Index");
}
关于c# - @Html.ActionLink 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20343148/