tags:

views:

524

answers:

2

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?

A: 

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).

Timothy Khouri
I checked both. When my HttpMethod is delete I am not seeing the __MVCASYNCPOST value come back in the request. Can you confirm that it works for you using HttpMethod.Delete? Then I can start trying to figure what is wrong with me ;)
spoon16
Just to be specific, this is the AcceptVerbs attribute I have on the executing action: [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Delete)]
spoon16
+2  A: 

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

tvanfosson
That worked, changing my Ajax.ActionLink to <%= Ajax.ActionLink("Delete Link", "Delete", new { graphUri = ViewData.Model.Uri.Value, __MVCASYNCPOST = "true" }, new AjaxOptions { Confirm = "Test", HttpMethod = "DELETE" })%> solved my issue. Thanks!
spoon16