Lucas Konrath
Lucas Konrath

Reputation: 577

Ajax.actionlink() not working with POST method (500 Internal Server Error)

sorry for my bad english, but let's to the problem.

i am trying to do a POST into the action complete of the controller occurrence, but the action don't receive the POST and the javascript console returns this error 500 Internal Server Error

This is my Ajax.actionlink()

@Ajax.ActionLink("Complete", "Complete", "Occurrence", new { id = Model.Id }, new AjaxOptions { HttpMethod = "POST" })

And this is my action Complete into the controller Occurrence

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult Complete(int id)
 {
     return new HttpStatusCodeResult(200);
 }

Someone has passed for the same situation? Thanks for the attention!

Upvotes: 1

Views: 1986

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

Your action is decorated with the [ValidateAntiForgeryToken] meaning that it will expect that the anti forgery token gets sent into the POST request payload. This isn't the case. In your request you are only sending some id (new { id = Model.Id }).

Once possible workaround is to use an Ajax.BeginForm instead which will contain the anti forgery token:

@using (Ajax.BeginForm("Complete", "Occurrence", new { id = Model.Id }, new AjaxOptions { HttpMethod = "POST" }))
{
    @Html.AntiForgeryToken()
    <button type="submit">Complete</button>
}

The Html.AntiForgeryToken() will generate a hidden field containing the required anti forgery token that will be sent along with the AJAX request to the server.

Upvotes: 2

Related Questions