c# - @Html.ActionLink 不工作

标签 c# asp.net-mvc razor orchardcms actionlink

我有一个 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/

相关文章:

c# - 实现 IXmlSerializable 需要集合属性具有 Setter

c# - 为什么 SignalR Context.User.Identity.Name 是空的?

asp.net - MVC 3 AjaxHelper Ajax.ActionLink 与 Ajax.RouteLink,Ajax.BeginForm 与 A​​jaxBeginRouteForm

c# - 使用 Razor/MVC3 将 AssemblyVersion 放入网页时出现问题

c# - 在 MVC 4 的部分 View 中使用 PagedList 在导航 'paged' 页面时抛出 jQuery/JS

c# - 文档 API : How to use Application Token

c# - .NET 配置类保存问题

c# - 最佳实践 boolean 逻辑倍数不等于

c# - ELMAH 异常生成通用 "The service is unavailable"消息

javascript - 无法通过 JQuery 更改 RadioButtonFor 选择