Eric Schoonover
Eric Schoonover

Reputation: 48402

Does Request.IsMvcAjaxRequest() method return true for requests originating from Ajax.ActionLink() click?

I have a controller action that is being executed by a link that was created using 'Ajax.ActionLink()' the problem is that I can't tell in my action that the request is an AJAX request because Request.IsMvcAjaxRequest() is always returning false.

Does Request.IsMvcAjaxRequest() not work with Ajax.ActionLink() generated requests?

Here is the code I am using to generate my link:

<%= Ajax.ActionLink("Delete", "Delete", new { graphUri = ViewData.Model.Uri.Value }, new AjaxOptions { Confirm = "Really delete?", OnSuccess = "success", OnFailure = "fail", HttpMethod = "DELETE" }, new { title = "Delete Graph", @class = "deleteGraphLink" })%>

When I look at the code for the IsMvcAjaxRequest extension method it looks like it will only work for AJAX Forms and not for AJAX ActionLinks.

Update 11/13

If I change the HttpMethod in the AjaxOptions to POST, all is well. Anyone know how to get Request.IsMvcAjaxRequest() to work properly when you are using the DELETE method?

Upvotes: 2

Views: 728

Answers (2)

tvanfosson
tvanfosson

Reputation: 532455

There are no form parameters on a delete. Try adding __MVCASYNCPOST=true as route data (query parameter in the url)

Upvotes: 2

Timothy Khouri
Timothy Khouri

Reputation: 31845

Because "IsMvcAjaxRequest" just returns (request["__MVCASYNCPOST"] == true), and that checks the query string, form post and cookies... it should work with POST,GET,etc.

Try putting a break point in your code behind and making sure it's hitting for the POST, then change to "DELETE" and see if it's hitting your code then (because this should work). It might be that you have an ActionFilter that's only allowing "POSTS" to get through (or something else that is thwarting the behavior).

Upvotes: 0

Related Questions