【问题标题】:@Html.ActionLink is not working@Html.ActionLink 不起作用
【发布时间】:2013-12-03 16:01:21
【问题描述】:

我有一个果园项目,我在 MVC 中创建了一个模块。我想使用 @Html.ActionLink 将特定用户的 id 传递给控制器​​,但它不调用控制器。这是我的代码:

在视图中:

 @Html.ActionLink("100111", "AddToCart", "ShoppingCart", new { id = 101 }, null)
//also tried,
@Html.ActionLink("102829", "AddToCart", "ShoppingCart", new { id = 1, area = "OnlineShopping" },null)

在控制器中:

[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]。由于[HttpPost],它没有调用控制器操作,并且单击锚标记实际上执行get。因此,请尝试从您的 AddToCart 操作中删除 [HttpPost] 属性装饰。

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


【解决方案1】:

由于[HttpPost],它没有调用操作方法,并且单击锚标记实际上会执行Get。因此,请尝试从您的 AddToCart 操作中删除 [HttpPost] 属性装饰。

    [HttpPost]//<--Remove this
    public ActionResult AddToCart(int id)
    {
        _shoppingCart.Add(id, 1);
        return RedirectToAction("Index");
    }

【讨论】:

    【解决方案2】:

    ASP.NET MVC ActionLink and post method找到这个

    您不能使用 ActionLink,因为它只会呈现锚标记。 您可以使用 JQuery AJAX 帖子,请参阅 http://docs.jquery.com/Ajax/jQuery.post 或者只是调用表单的提交 有或没有 JQuery 的方法(这将是非 AJAX),也许在 任何你喜欢的控件的 onclick 事件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-29
      • 2021-05-16
      • 2021-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多