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);
}
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");
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.