@Html.ActionLink is not working

4.2k views Asked by At

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);
    }
2

There are 2 answers

0
PSL On BEST ANSWER

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");
    }
0
Nitin Varpe On

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.