Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Ajax.ActionLink doesn't find POST method

I have a POST method declared in my controller:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateComments(int id, string comments)
{
    // ... 
}

and an ActionLink in my view:

<%= Ajax.ActionLink("update", "UpdateComments", 
                        new { id = Model.Id, comments = "test" }, 
                        new AjaxOptions { 
                                HttpMethod="POST", 
                                OnFailure="alert('fail');", 
                                OnSuccess = "alert('success');" 
                            })%>

I get a "not found" error when it tries to route this request.

If I remove the POST restriction from the UpdateComments method in the controller, it works fine.

What am I missing?

like image 872
fearofawhackplanet Avatar asked Jun 14 '10 11:06

fearofawhackplanet


2 Answers

It seems it didn't like the way I was declaring my OnFailure and OnSuccess callbacks. I guess it couldn't parse my AjaxOptions object so was ignoring the HttpMethod="POST" setting.

I got it working by changing it to:

OnFailure="function() { alert('fail'); }",
OnSuccess="function() { alert('success'); }" 
like image 81
fearofawhackplanet Avatar answered Sep 30 '22 08:09

fearofawhackplanet


I am learning ASP.MVC at this moment and I had that issue with my Ajax.ActionLink, I got a GET method and not a POST method as it should had been. Then I realize that I didn't add the scripts library reference:

<script src=”/Scripts/MicrosoftAjax.js” type=”text/javascript”></script>
<script src=”/Scripts/MicrosoftMvcAjax.js” type=”text/javascript”></script>

After I adding the script it worked fine!

like image 27
Carlos Avatar answered Sep 30 '22 07:09

Carlos