0

How do I call the second action Index below

public ActionResult Index()
{
}

[HttpPost]
public ActionResult Index(FormCollection collection, string nextButton)
{
}

from an ActionLink? I am trying the code below without success:

@Html.ActionLink("Buy Now", "Index", "Store", new {edition = "std", nextButton = ""}, new Dictionary<string, object> {{ "class", "button medium light" }})

Thanks.

abenci
  • 8,422
  • 19
  • 69
  • 134
  • 2
    Duplicate of [ASP.NET MVC ActionLink and post method](http://stackoverflow.com/questions/2048778/asp-net-mvc-actionlink-and-post-method). – CodeCaster Mar 21 '13 at 09:53

1 Answers1

1

The ActionLink by default with generate a anchor link, thus when clicked will perform a GET request to the endpoint.

You can use jquery to perform a post to the action endpoint asynchronously using ajax.

$.ajax({ 
    url: 'http://endpoint.com', 
    type: 'POST', 
    data: $('#form').serialize() //Add some post data however you want. 
});

Or you can post the endpoint using a form. You should also decorate your endpoint with [ValidateAntiForgeryToken] if using a form. If using the jquery way of posting, you can still add the AntiForgery hidden filed as a header on the post and validate by checking the header in a custom filter.

@using Html.BeginForm() {
    @Html.AntiForgeryToken()
    //Add some inputs to represent your model
    <button type="submit">Save</button>
}
gdp
  • 8,032
  • 10
  • 42
  • 63