1

I have a orchard project, where I have created a module in MVC. I want to pass the id of particular user to controller using @Html.ActionLink but it not calling controller. Here is my code:

In view:

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

In 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);
    }
Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
Bhupendra Shukla
  • 3,814
  • 6
  • 39
  • 62
  • 2
    You may want to remove `[HttpPost]`. It is not calling the controller action because of `[HttpPost]` and clicking on anchor tag actually does a `get`. So try remove the `[HttpPost]` attribute decoration from your `AddToCart` action. – PSL Dec 03 '13 at 05:02

2 Answers2

8

It is not calling the action method because of [HttpPost] and clicking on anchor tag actually does a Get. So try remove the [HttpPost] attribute decoration from your AddToCart action.

    [HttpPost]//<--Remove this
    public ActionResult AddToCart(int id)
    {
        _shoppingCart.Add(id, 1);
        return RedirectToAction("Index");
    }
PSL
  • 123,204
  • 21
  • 253
  • 243
0

Found this on ASP.NET MVC ActionLink and post method

You can't use an ActionLink because that just renders an anchor tag. You can use a JQuery AJAX post, see http://docs.jquery.com/Ajax/jQuery.post or just call the form's submit method with or without JQuery (which would be non-AJAX), perhaps in the onclick event of whatever control takes your fancy.

Community
  • 1
  • 1
Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60