Ajax.ActionLink is not POSTing
Asked Answered
H

2

7

I am trying to navigate to an MVC action by POSTing rather than GETting. (The action is a DELETE, and I don't want it reachable by an external link.)

I am using a link in a grid generated by

 Ajax.ActionLink("Remove", "Delete", new { saID = Model.Said, id = e.id }, new AjaxOptions { HttpMethod = "POST", Confirm = "Are you sure you want to delete this item?" })

Which generates the following HTML:

<a href="/Equipment/Delete/102424/229933" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, confirm: 'Are you sure you want to delete this item?', httpMethod: 'POST' });">Remove</a>

My problem is that when I click on the link, I am reaching the Delete action via a GET rather than a POST, AND the Confirm dialog is not taking place. I have been googling this for several hours and just keep getting wrapped around the axle. What am I doing wrong?

Heed answered 17/6, 2010 at 20:48 Comment(0)
S
6

It's likely that you have some other javascript error on your page that is preventing the javascript that handles the AJAX POST from running. In this case, the link falls back to it's default behavior (GET). The easiest thing to do is use the IE8 developer tools or Firefox/Firebug (I prefer these) and look to see if you have any errors in the console when the page loads or your action is invoked. If you are using IE you'll need to use Internet Options -> Advanced and uncheck Disable script debugging.

Fix your javascript error and I think it will simply start working.

Suppositive answered 17/6, 2010 at 21:2 Comment(3)
Your right: I'm getting "Sys.Mvc" is undefined - even though I've included all three of the JS script files in my MasterPage (MicrosoftAjax, MicrosoftMvcAjax, and jquery-1.4.1-vsdoc) What else do I need to do to get Sys.Mvc defined?Heed
@Dave - are you including them in that order? Sys.Mvc is defined in MicrosoftMvcAjax.js but it does so by calling a method from MicrosoftAjax.jsSuppositive
That appears to be the problem. When I hooked fiddler up to it, only the first (MicrosoftAjax.js) of the three script files was loading. I fixed that so that MicrosoftMvcAjax.js (which contains the definition of Sys.Mvc) was loading, and it did take off and run - well, sorta. I'm getting the confirm box now, but I'm not getting my Delete form. But I'll look into that tomorrow.Heed
A
5

I wanted to add this as a comment under accepted answer but somehow there is no option to enter this as a comment so adding this as answer

In my case, I had to add "jquery.unobtrusive-ajax.min.js" before MicrosoftAjax.js and MicrosoftMvcAjax.js and then action link started initiating ajax request. But this was a GET and I wanted post so I just added HttpMethod = "Post" in the 'AjaxOptions`. That was it.

Archimedes answered 4/10, 2011 at 7:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.